【问题标题】:MessageBodyWriter not found for media type=Application/json, glassfish未找到媒体类型 = 应用程序/json、glassfish 的 MessageBodyWriter
【发布时间】:2013-11-20 01:31:55
【问题描述】:

我正在使用 JAX-RS 创建简单的 restful json,我的第一个方法工作正常,但是当我添加第二个来获取所有 vendorNOS"ID" 方法时,当我在浏览器中查看时出现此异常,我还调试了 Restful 服务和它工作正常它凝胶所有供应商NOS“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;
    }
}

【问题讨论】:

    标签: java json web-services glassfish


    【解决方案1】:

    使用 genson (https://code.google.com/p/genson/downloads/list) jar 并将其添加到类路径中。这会将任何对象转换为 json 格式。您收到此错误是因为您没有 json 提供程序。并且最好返回 object 而不是 toString()。

    您还可以使用球衣捆绑包附带的 JAXB jar。这将同时支持 XML 和 JSON。您可以在 jersey 发行版的 /ext 文件夹中找到该 jar。

    【讨论】:

      【解决方案2】:

      我通过添加以下依赖解决了它。

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

      我正在使用 jersey-spring3jersey 2spring4

      【讨论】:

      • 他专门询问 Glassfish,它是一个完整的 Java EE 容器。
      【解决方案3】:

      List&lt;Integer&gt; 似乎是您从第二种方法返回的问题。

      您是否看到类似以下的错误?

      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。此外,这似乎是duplicate

      【讨论】:

      • 我将方法返回类型更新为 String 并转换 vendorFacadeBean.getVendorsnos().toString();我会尽快更新我的答案
      猜你喜欢
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      • 2014-12-06
      • 2014-06-29
      • 2015-12-13
      • 2017-07-24
      • 2021-06-15
      • 2015-05-22
      相关资源
      最近更新 更多