[英]Unsupported MEDIA TYPE: 415 error Spring-rest webservices
I created a restful web application using spring-rest api. 我使用spring-rest api创建了一个宁静的Web应用程序。 I get unsupported media type error when I try to run my application from post-mater chrome plugin or Advanced rest client.
尝试从事后chrome插件或Advanced rest客户端运行应用程序时,出现不受支持的媒体类型错误。
I am posting the different files as follows: 我要发布不同的文件,如下所示:
1) web.xml 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 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 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) Employee bean 4)员工豆
public class Employee {
private int employeeId;
private String employeeName;
}
How I am sending the data using post master 我如何使用Post Master发送数据
Where am I going wrong? 我要去哪里错了? I tried almost everything :(
我尝试了几乎所有东西:(
Error code 415
is Unsupported Media Type
, meaning it doesn't understand plain text vs json vs xml. 错误代码
415
是Unsupported Media Type
,这意味着它不理解纯文本,json和xml。 You should put, in your @RequestMapping
on the method 您应该在方法的
@RequestMapping
放置
produces = {MediaType.APPLICATION_JSON_VALUE}
and 和
consumes = {MediaType.APPLICATION_JSON_VALUE}
Then, in your client make sure in the headers you put Accept = application/json
and Content-type = application/json
. 然后,在客户端中,确保在标头中放置了
Accept = application/json
和Content-type = application/json
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.