不支持的媒体类型:415错误Spring-rest webservices

时间:2015-08-01 10:39:51

标签: java spring web-services rest spring-mvc

我使用spring-rest api创建了一个安静的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;
}

我如何使用邮政主文件发送数据

enter image description here

我哪里错了?我几乎尝试了一切:(

1 个答案:

答案 0 :(得分:-1)

错误代码415Unsupported Media Type,这意味着它不了解纯文本vs json vs xml。你应该把@RequestMapping放在方法

produces = {MediaType.APPLICATION_JSON_VALUE}

consumes = {MediaType.APPLICATION_JSON_VALUE}

然后,在您的客户端中,请确保在标题Accept = application/jsonContent-type = application/json中添加。