我有一个简单的泽西服务下面的代码:泽西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>
您是否获得了服务器上的任何错误响应数据,或任何堆栈跟踪?这通常是杰克逊错误,默认情况下,您应该收到异常错误消息作为响应。如果你已经覆盖了这个行为,那么撤消它找出错误 –
不,我没有覆盖任何东西。我从cURL获得了400个响应,而在服务器上完全没有。对于成功的请求,我在服务器的控制台上获取我的日志消息。查看我对应用程序设置的编辑,但我没有看到任何会吞噬异常的东西,我的所有日志消息都看起来不错。 – mikeb