Java 未找到 Media type=Application/json、glassfish 的 MessageBodyWriter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20085687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MessageBodyWriter not found for media type=Application/json, glassfish
提问by
I'm using JAX-RS to create simple restful json my first method work fine but when I add 2nd to get all vendorNOS"ID" method I have this exception when I view the in browser I also debug the Restful service and it work fine it gell all vendorNOS"ID"
我正在使用 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 source code
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;
}
}
回答by malatesh
The problem seems with List<Integer>
that you are returning from second method.
问题似乎List<Integer>
在于您从第二种方法返回。
Are you seeing error like the following?
您是否看到如下错误?
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
Please refer to GenericEntity. Moreover this seems duplicate.
请参考GenericEntity。而且这似乎是重复的。
回答by Akalanka
Use genson (https://code.google.com/p/genson/downloads/list) jar and add this to class path. This will convert any object to json format. You get this error because you do not have a json provider. And it is better to return object rather than toString().
使用 genson ( https://code.google.com/p/genson/downloads/list) jar 并将其添加到类路径。这会将任何对象转换为 json 格式。您收到此错误是因为您没有 json 提供程序。最好返回对象而不是 toString()。
As well as you can use JAXB jar which comes with jersey bundle. This will support both XML and JSON. You can find the jar inside /ext folder of jersey distribution.
你也可以使用 jersey bundle 附带的 JAXB jar。这将支持 XML 和 JSON。您可以在 jersey 发行版的 /ext 文件夹中找到该 jar。
回答by Taher
I solved it by adding the following dependency.
我通过添加以下依赖项解决了它。
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.17</version>
</dependency>
I am using jersey-spring3, jersey 2and spring4.
我正在使用jersey-spring3、jersey 2和spring4。