我正在使用带有 xml/json 对象的 spring MVC,并且出现以下错误:
HTTP Status 400 - The request sent by the client was syntactically incorrect ().
这是我的控制器。
@RequestMapping(method=RequestMethod.POST, value="/emp")
public @ResponseBody EmployeeList addEmp(@RequestBody Employee e) {
employeeDS.add(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}
@RequestMapping(method=RequestMethod.PUT, value="/emp/{id}")
public @ResponseBody EmployeeList updateEmp(@RequestBody Employee e, @PathVariable String id) {
employeeDS.update(e);
List<Employee> employees = employeeDS.getAll();
EmployeeList list = new EmployeeList(employees);
return list;
}
我正在尝试使用 curl 发送 JSON 对象:
curl -v -X PUT -HContent-type:application/json --data '{"id":3,"name":"guest","email":"guest@ibm.com"}' http://localhost:8080/rest/service/emp/1
curl -v -X POST -HContent-type:application/json --data '{"id":3,"name":"guest","email":"guest@ibm.com"}' http://localhost:8080/rest/service/emp
我在 pom 文件中添加了以下杰克逊依赖项:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.12</version>
</dependency>
我还在 servlet-dispatcher.xml 中添加了以下配置:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml"/>
<entry key="json" value="application/json" />
<!--entry key="html" value="text/html"/-->
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.mkyong.common.bean.Employee</value>
<value>com.mkyong.common.bean.EmployeeList</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="false" />
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="marshallingConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
下面是 Employee 类的样子:
package com.mkyong.common.bean;
导入 javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="employee") 公共类员工 {
private long id;
private String name;
private String email;
public Employee() {}
public Employee(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
我的库中有以下罐子:
aopalliance-1.0.jar
aspectjrt-1.5.3.jar
aspectjweaver-1.5.3.jar
axiom-api-1.2.7.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
jackson-core-asl-1.9.13.jar
jackson-jaxrs-1.9.12.jar
jackson-mapper-asl-1.9.13.jar
jaxb-api-2.1.jar
jaxb-impl-2.2.jar
joda-time-1.6.2.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
slf4j-api-1.7.2.jar
spring-aop-3.1.3.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-context-support-3.1.3.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-oxm-3.2.2.RELEASE.jar
spring-security-config-3.1.3.RELEASE.jar
spring-security-core-3.1.3.RELEASE.jar
spring-security-web-3.1.3.RELEASE.jar
spring-tx-3.1.3.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.1.3.RELEASE.jar
spring-ws-core-2.1.2.RELEASE.jar
spring-ws-security-2.1.2.RELEASE.jar
spring-xml-2.1.2.RELEASE.jar
stax-api-1.0-2.jar
wsdl4j-1.6.1.jar
wss4j-1.6.5.jar
xmlsec-1.5.1.jar
xmlsec-2.0.jar
xmltooling-1.3.2-1.jar
xws-security-1.3.1.jar
我的 GET 适用于 XML 和 JSON:
@RequestMapping(method=RequestMethod.GET, value="/emp/{id}", headers="Accept=application/xml, application/json")
public @ResponseBody Employee getEmp(@PathVariable String id) {
Employee e = employeeDS.get(Long.parseLong(id));
return e;
}
对于以下 curl 命令:
curl -HAccept:application/xml http://localhost:8080/rest/service/emp/1
curl -HAccept:application/json http://localhost:8080/rest/service/emp/1