[英]HTTP/1.1 415 Unsupported Media Type while processing JSON
My code is working with Xml but fails with Json 我的代码在Xml上运行,但在Json上失败
Request with RestEasy UI: 使用RestEasy UI的请求:
POST /api/people HTTP/1.1
Content-Type:application/json
{
"name":"developer",
"age":"25",
"address":"address"
}
Class Resource.java 类Resource.java
package people;
@Path("/api")
public class Resource {
@GET
@Produces("text/plain")
@Path("helloworld")
public String helloWorld() {
return "Hello World!";
}
@POST
@Produces("text/plain")
@Consumes(MediaType.APPLICATION_JSON)
@Path("people")
public String updatePeople(People request) {
People p = new People();
p.setName(request.getName());
return p.getName();
}
}
and class People.java 和类People.java
package people;
@XmlRootElement
public class People {
private String name;
private String age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
add default constructor in bean class People. 在bean类People中添加默认构造函数。
public people(){
}
Add the following jar to your class path. 将以下jar添加到您的类路径中。 jersey-media-json-jackson.jar
jersey-media-json-jackson.jar
If you are using Maven, add this to your POM.xml file. 如果使用的是Maven,请将其添加到POM.xml文件中。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
Probably you are facing the following exception on the server side: 您可能在服务器端面临以下异常:
javax.ws.rs.NotSupportedException: RESTEASY003200: Could not find message body reader for type: class people.People of content type: application/json
javax.ws.rs.NotSupportedException:RESTEASY003200:找不到类型为:人的消息正文阅读器。内容类型为:application / json的人
In this case you need to add the resteasy-jackson2-provider
dependency to your project ; 在这种情况下,您需要向项目添加
resteasy-jackson2-provider
依赖项 ; so, assuming that you are using Maven, try putting this into your pom.xml
: 因此,假设您正在使用Maven,请尝试将其放入
pom.xml
:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>3.6.0.Final</version>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.