我使用spring-rest api创建了一个rest风格的web应用程序。当我尝试从post-mater chrome插件或Advanced rest客户端运行我的应用程序时,我得到了不支持的媒体类型错误。
我将不同的文件张贴如下:
1) web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>2) rest-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="com.mayank.spring.mvc" />
<mvc:annotation-driven />
</beans>3)控制器
@Controller
@RequestMapping("/service/greeting")
public class SpringServiceController {
@RequestMapping(value = "/", method = RequestMethod.POST)
public @ResponseBody Employee getGreeting(@RequestBody Employee name) {
if(name!=null){
name.setEmployeeId(name.getEmployeeId()+1);
name.setEmployeeName(name.getEmployeeName());
}
return name;
}
}4)员工bean
public class Employee {
private int employeeId;
private String employeeName;
}我如何使用post master发送数据

我哪里错了?我几乎什么都试过了:
发布于 2016-02-06 02:14:55
错误代码415是Unsupported Media Type,这意味着它不能理解纯文本、json和xml。您应该在方法的@RequestMapping中放入
produces = {MediaType.APPLICATION_JSON_VALUE}和
consumes = {MediaType.APPLICATION_JSON_VALUE}然后,在你的客户端中,确保在头文件中放入Accept = application/json和Content-type = application/json。
https://stackoverflow.com/questions/31760925
复制相似问题