找不到媒体类型= application / json的MessageBodyWriter,type = class SalesService.Item,genericType = class SalesService.Item

时间:2017-03-04 19:43:38

标签: java json jax-rs jersey-2.0 restful-architecture

我收到此错误:找不到媒体类型= application / json的MessageBodyWriter,type = class SalesService.Item,genericType = class SalesService.Item。

我想要做的是让我的restful服务方法返回一个自定义对象,在这种情况下对象:Item。但我得到这个错误,并在浏览器错误500。

我在netbeans中使用JAX-RS 2.25.1版本库

我知道有很多像这样的问题,但我找不到任何适合我的解决方案。

提前致谢!

WEB.XML

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">
<display-name>examples.rest.school</display-name>
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>SalesService</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>

资源服务类:

package SalesService;

import java.util.ArrayList;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import DB.DBConnecter;
import javax.ws.rs.PathParam;
import java.util.List;
import javax.ws.rs.Consumes;


@Path("Sales")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class SalesResources {

private final DBConnecter dbConnector;

public SalesResources() {
    dbConnector = new DBConnecter();
}


@GET
@Path("session/{sessionID}")
@Produces(MediaType.APPLICATION_JSON)
public Item GetItem(@PathParam("sessionID") String sessionID) {
    List<Item> items = new ArrayList<>();
    if(dbConnector.CheckSessionID(sessionID)){
        items = dbConnector.GetItems();
        return items.get(0);
    }
    return new Item();
}
}

项目类:

package SalesService;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class Item {
private int id;
private String name;
private int quantity;
private double price;
private String type;

public Item(){}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getQuantity() {
    return quantity;
}

public void setQuantity(int quantity) {
    this.quantity = quantity;
}

public double getPrice() {
    return price;
}

public void setPrice(double price) {
    this.price = price;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public void AddQuantity() {
    this.quantity = quantity + 1;
}

public void RemoveQuantity() {
    this.quantity = quantity - 1;
}

public Item(int id, String name, int quantity, double price, String type) {
    this.id = id;
    this.name = name;
    this.quantity = quantity;
    this.price = price;
    this.type = type;
}
}

栈跟踪

04-Mar-2017 21:03:06.273 SEVERE [http-nio-8080-exec-1] org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo MessageBodyWriter not found for media type=application/json, type=class SalesService.Item, genericType=class SalesService.Item.

1 个答案:

答案 0 :(得分:0)

添加

后解决了这个问题
org.codehaus.jackson.jaxrs

在像这样的web.xml中

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 version="3.1">
<display-name>examples.rest.school</display-name>
<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>SalesService,org.codehaus.jackson.jaxrs</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>

添加4个杰克逊.JAR文件后,这些

enter image description here

并将它们添加到库文件夹(参考)