找不到 Media type=application/json 的 MessageBodyWriter [英] MessageBodyWriter not found for media type=application/json
问题描述
我在将 JAX-RS 服务作为 JSON 使用时遇到问题.
I am facing issues while consuming JAX-RS services as JSON.
下面我添加了我的代码.
Below I have added my code.
这是我的服务类:
//Sets the path to base URL + /hello
@Path("/hello")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class Hello {
@GET
@Produces("application/json")
public Student getStudent() {
Student s = new Student();
s.first_name = "Test First Name !!!";
s.last_name = "Test Last Name!!!";
return s;
}
Student
我试图从服务中获取的类:
Student
class which I am trying to get from service:
@XmlRootElement
public class Student implements Serializable {
public String first_name;
public String last_name;
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public Student()
{
first_name = "Fahad";
last_name = "Mullaji";
}
}
服务端的 Web XML.
Web XML on service side.
<?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"
id="WebApp_ID" version="3.0">
<display-name>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.vogella.jersey.first</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我不知道如何解决这个问题.我正在使用 SOAP UI 来测试 JSON 响应,但我想这应该无关紧要.
I don't know how to fix this issue. I am using SOAP UI for testing JSON response but I guess that it should not matter.
我读到的很多地方都需要在下面添加代码.但我不知道在哪里.我没有使用 Maven 进行构建.
Many places I read that I need to add the code below. But I don't know where. I am not using Maven for building.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
服务器正在寻找将 Student
对象解析为 JSON 的函数,但无法为其找到函数或 jar 文件.我添加了 jar 的 Genson、Moxy、RESTEasy 和 Jackson,但我认为这就是问题所在.我想我在某处缺少映射.
Server is looking for function to parse Student
object to JSON but it is not able to find function or jar file for it.
I have added jar of Genson, Moxy, RESTEasy and Jackson but I think that's the problem.
I think I am missing mapping somewhere.
推荐答案
我能够通过安装 jersey-media-json-jackson 来修复它
I was able to fix it by install jersey-media-json-jackson
将依赖添加到 pom.xml
Add the dependency to pom.xml
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<scope>runtime</scope>
</dependency>
这篇关于找不到 Media type=application/json 的 MessageBodyWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!