Java – messagebodywriter with media type = Application / JSON, GlassFish not found

I am using jax-rs to create a simple restful JSON. My first method works normally, but when I add the second method to get all vendornos "Id" methods, when I view this exception in the browser, I also debug the restful service and it works normally. It Gell all 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,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 source code

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;
    }
}

Solution

Use genson( https://code.google.com/p/genson/downloads/list )Jar and add it to the classpath This converts any object to JSON format You receive this error because you do not have a JSON provider It's better to return an object instead of toString ()

In addition, you can use the JAXB jar. Jar included with the Jersey bundle This will support XML and JSON You can find the jar / ext folder in Jersey distribution

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>