我在简单的泽西服务中有以下代码:
@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
这在Jetty中运行,如果重要的话
编辑这是应用程序配置:
@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)
请尝试以下列格式传递您的数据。
{&#34; formValues&#34;:[{&#34; fieldName&#34;:&#34; asdf&#34;,&#34; fieldValue&#34;:&#34; asdf&#34; ,&#34; fieldType&#34;:&#34; CHAR&#34;}]}
答案 1 :(得分: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>
使init 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>