首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不支持的媒体类型: 415错误Spring-rest webservices

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

Stack Overflow用户
提问于 2015-08-01 18:39:51
回答 1查看 2.1K关注 0票数 0

我使用spring-rest api创建了一个rest风格的web应用程序。当我尝试从post-mater chrome插件或Advanced rest客户端运行我的应用程序时,我得到了不支持的媒体类型错误。

我将不同的文件张贴如下:

1) web.xml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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)控制器

代码语言:javascript
复制
@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

代码语言:javascript
复制
public class Employee {
private int employeeId;
private String employeeName;
}

我如何使用post master发送数据

我哪里错了?我几乎什么都试过了:

EN

回答 1

Stack Overflow用户

发布于 2016-02-06 02:14:55

错误代码415Unsupported Media Type,这意味着它不能理解纯文本、json和xml。您应该在方法的@RequestMapping中放入

代码语言:javascript
复制
produces = {MediaType.APPLICATION_JSON_VALUE}

代码语言:javascript
复制
consumes = {MediaType.APPLICATION_JSON_VALUE}

然后,在你的客户端中,确保在头文件中放入Accept = application/jsonContent-type = application/json

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31760925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档