java – 找不到媒体类型= Application / json,glassfish的Messag
发布时间:2020-12-15 04:15:31 所属栏目:Java 来源:网络整理
导读:我正在使用JAX-RS来创建简单的restful json我的第一个方法正常工作但是当我添加第二个来获取所有vendorNOS“ID”方法时,我在浏览器中查看此异常时我也调试了Restful服务并且它工作正常它gell所有vendorNOS“ID” my output from vendorFacadeBean is {1,2,3,
我正在使用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,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文件夹. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |