gpt4 book ai didi

java - 找不到媒体类型 MessageBodyWriter=Application/json,glassfish

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:32 27 4
gpt4 key购买 nike

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

您还可以使用 Jersey 包附带的 JAXB jar。这将同时支持 XML 和 JSON。您可以在 jersey 分发的/ext 文件夹中找到 jar。

关于java - 找不到媒体类型 MessageBodyWriter=Application/json,glassfish,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20085687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com