簡體   English   中英

找不到媒體類型= Application / json,glassfish的MessageBodyWriter

[英]MessageBodyWriter not found for media type=Application/json, glassfish

我正在使用JAX-RS來創建簡單的restful json我的第一個方法正常工作但是當我添加第二個來獲取所有vendorNOS“ID”方法時,我在瀏覽器中查看此異常時我也調試了Restful服務並且它工作正常它gell所有vendorNOS“ID”

my output from vendorFacadeBean is {1,2,3,4,5,6,11,13}

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

    javax.servlet.ServletException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=Application/json, type=class java.util.Vector, genericType=java.util.List<java.lang.Integer>.
    root cause

    org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=Application/json, type=class java.util.Vector, genericType=java.util.List<java.lang.Integer>.
    note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.

    GlassFish Server Open Source Edition 4.0

java源代碼

package resources;

import case2dtos.VendorEJBDTO;
import case2ejbs.VendorFacadeBean;
import java.util.List;
import javax.ejb.EJB;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;

/**
 * REST Web Service
 *
 * @author abdallaelnajjar
 */
@Path("vendors")
@RequestScoped
public class VendorsResource {
    @EJB
    private VendorFacadeBean vendorFacadeBean;

    @Context
    private UriInfo context;

    /**
     * Creates a new instance of VendorsResource
     */
    public VendorsResource() {
    }

    /**
     * Retrieves representation of an instance of resources.VendorsResource
     * @return an instance of java.lang.String
     */
    @GET
    @Path("getAVendor/{vendorno}")
    @Produces("Application/json")
    public VendorEJBDTO getAVendor(@PathParam("vendorno")int vendorno)
    {
        return vendorFacadeBean.getVendorInfo(vendorno);
    }

    /**
     * Retrieves representation of an instance of resources.VendorsResource
     * @return an instance of java.lang.String
     */
    @GET
    @Path("getVendornos")
    @Produces("Application/json")
    public List<Integer> getVendornos()
    {
        List<Integer> vendornosList = null;
        try 
        {
         vendornosList =  vendorFacadeBean.getVendorsnos();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }

        return vendornosList;
    }
}

使用genson( https://code.google.com/p/genson/downloads/list )jar並將其添加到類路徑。 這會將任何對象轉換為json格式。 您收到此錯誤,因為您沒有json提供程序。 返回對象而不是toString()更好。

除了你可以使用澤西束附帶的JAXB jar。 這將支持XML和JSON。 你可以找到澤西分布的jar里面/ ext文件夾。

我通過添加以下依賴項解決了它。

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.17</version>
</dependency>

我使用的是jersey-spring3jersey 2spring4

List<Integer>的問題似乎是你從第二種方法返回。

您是否看到如下錯誤?

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/xml was not found

請參閱GenericEntity 此外,這似乎重復

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM