2017-02-20 53 views
0

我有一个简单的泽西服务下面的代码:泽西1.19返回400错误传递一个数组转换成Web服务

@Path("/form") 
@Consumes(MediaType.APPLICATION_JSON) 
public class FormValueService { 
    private static final Logger L = LoggerFactory.getLogger(FormValueService.class); 

    public FormValueService() { 
     L.info("Creating FVS"); 
    } 

    @POST 
    @Path("/values") 
    @Produces({MediaType.TEXT_PLAIN}) 
    public String saveFormData(FormValue[] formValues) { 
     String formId = UUID.randomUUID().toString(); 
     L.debug("Saving form {}", formId); 
     for (FormValue fv : formValues) { 
      L.debug(" Form value is {}", fv); 
     } 
     L.debug("Finished saving form {}", formId); 
     return String.valueOf(Boolean.TRUE); 

    } 

    @POST 
    @Path("/value") 
    @Produces({MediaType.TEXT_PLAIN}) 
    public String saveFormData(FormValue formValue) throws IOException { 
     java.util.logging.LogManager.getLogManager().readConfiguration(this.getClass().getClassLoader().getResourceAsStream("logging.properties")); 
     String formId = UUID.randomUUID().toString(); 
     L.debug("Saving form {}", formId); 
     L.debug(" Form value is {}", formValue); 
     L.debug("Finished saving form {}", formId); 
     return String.valueOf(Boolean.TRUE); 
    } 
} 

第二种方法的伟大工程:

curl -i -X POST -d '{"fieldName": "asdf", "fieldValue": "asdf", "fieldType": "CHAR"}' -H "Accept: text/plain" -H "Content-Type: application/json" localhost:8080/ManageContact/rest/form/value

没有问题在这里,我得到正确的服务器消息。

第一方法总是返回400错误:

curl -i -X POST -d '[{"fieldName": "asdf", "fieldValue": "asdf", "fieldType": "CHAR"}]' -H "Accept: text/plain" -H "Content-Type: application/json" localhost:8080/ManageContact/rest/form/values

这在码头运行时,如果该事项

EDIT这里是应用程序配置:

@ApplicationPath("rest") 
public class ContactApplication extends Application{ 
    Logger logger = LoggerFactory.getLogger(ContactApplication.class); 

    @Override 
    public Set<Object> getSingletons() { 
     logger.debug("Entering ContactApplication.getSingletons()"); 
     EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("MyJPAJAXRS"); 
     ContactService contactService = new ContactService(entityManagerFactory); 
     FormValueService formValueService = new FormValueService(); 

     Set<Object> singletons = new HashSet<Object>(); 
     singletons.add(contactService); 
     singletons.add(formValueService); 
     logger.debug("Exiting ContactApplication.getSingletons()"); 
     return singletons; 
    } 

} 

另外,这里是log4j属性:

log4j.rootLogger=DEBUG, A1, LogFileAppender 
log4j.appender.A1=org.apache.log4j.ConsoleAppender 
log4j.appender.A1.layout=org.apache.log4j.PatternLayout 

# Print the date in ISO 8601 format 
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n 

这里是logging.properties

com.sun.jersey.level=ALL 
com.sun.jersey.handlers=java.util.logging.ConsoleHandler 
java.util.logging.ConsoleFormatter.formatter=java.util.logging.SimpleFormatter 

这里是我如何与mvn jetty:run启动它:

<plugin> 
       <groupId>org.eclipse.jetty</groupId> 
       <artifactId>jetty-maven-plugin</artifactId> 
       <version>${jetty.plugin.version}</version> 
       <configuration> 
        <systemProperties> 
         <systemProperty> 
          <name>java.util.logging.config.file</name> 
          <value>logging.properties</value> 
         </systemProperty> 
        </systemProperties> 
        <scanIntervalSeconds>10</scanIntervalSeconds> 
        <webAppConfig> 
         <contextPath>/ManageContact</contextPath> 
        </webAppConfig> 
        <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog"> 
         <filename>target/yyyy_mm_dd.request.log</filename> 
         <retainDays>90</retainDays> 
         <append>true</append> 
         <extended>true</extended> 
         <logTimeZone>GMT</logTimeZone> 
        </requestLog> 
       </configuration> 
      </plugin> 
+0

您是否获得了服务器上的任何错误响应数据,或任何堆栈跟踪?这通常是杰克逊错误,默认情况下,您应该收到异常错误消息作为响应。如果你已经覆盖了这个行为,那么撤消它找出错误 –

+0

不,我没有覆盖任何东西。我从cURL获得了400个响应,而在服务器上完全没有。对于成功的请求,我在服务器的控制台上获取我的日志消息。查看我对应用程序设置的编辑,但我没有看到任何会吞噬异常的东西,我的所有日​​志消息都看起来不错。 – mikeb

回答

0

于是,我找到了答案,如果任何人发现这个。问题在于我的web.xml中的servlet init。我不得不添加一个init-param告诉球衣启用POJO Mapping功能:

<init-param> 
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
     <param-value>true</param-value> 
    </init-param> 

使得初始化servlet的这个样子的:

<display-name>Archetype Created Web Application</display-name> 
    <servlet> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
       <param-name>javax.ws.rs.Application</param-name> 
       <param-value>com.webspherenotes.rest.ContactApplication</param-value> 
     </init-param> 
     <init-param> 
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
      <param-value>true</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
0

请尝试通过以下格式的数据。

{ “formValues”:[{ “字段名”: “ASDF”, “fieldValue方法”: “ASDF”, “字段类型”: “CHAR”}]}

+0

不,不是,但谢谢你的答案。如果你有一个包含'List '的成员的包装对象,那么就可以这样做,但我应该可以使用数组。 – mikeb

+0

formvalue是你的自定义类型(类),所以我们需要提供名称请尝试如果作品或没有请告知 – arjun

+0

不,如我所说,方法采用'FormValue []',它应该只是一个JSON数组,而不是JSON像你提议的对象。 – mikeb

相关问题