简体   繁体   English

无法返回 rest api 中媒体类型“application/xml”的对象列表

[英]Unable to return List of objects for the mediatype “application/xml” in rest api

Am trying to return list of object on HTTP GET request for the media type "application/xml".我试图在 HTTP GET 请求中返回 object 的列表,以获取媒体类型“application/xml”的请求。

Am getting below error:出现以下错误:

SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=class java.util.ArrayList. SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=class java.util.ArrayList.

Tried below things:尝试了以下事情:

1) My DTO has no arg constructor and implements seralizable. 1) 我的 DTO 没有 arg 构造函数并实现了可序列化。 2) I have jersey jaxb dependencies in my pom to do xml marshall/unmarshall. 2)我的 pom 中有 jersey jaxb 依赖项来执行 xml 编组/解组。

Note : It does work with returning single object but not with List of objects注意:它适用于返回单个 object 但不适用于对象列表

This might be a silly mistake.这可能是一个愚蠢的错误。

@XmlRootElement
public class Company implements Serializable  {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String car;
    private String brand;

    public Company() {

    }
 ...... genereated setter/getter

I was getting List of objects from database.我正在从数据库中获取对象列表。

// values getting from database.
    List<Company> companyList = new ArrayList<Company>(values);
    return Response.status(Status.OK).entity(companyList).build();

Help is Appreciated!帮助表示赞赏!

I was able to resolve this by adding GenericEntity and passing the list to it.我能够通过添加 GenericEntity 并将列表传递给它来解决这个问题。

GenericEntity<List<Company>> companyEntity = new GenericEntity<List<Company>>(companyList){};
return Response.status(Status.OK).entity(companyEntity).build();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Spring Boot Rest API 中以 XML 形式返回对象列表 - How to return a list of objects as XML in Spring boot rest API Jersey REST客户端 - 将自定义MediaType视为MediaType.APPLICATION_JSON - Jersey REST Client - Treat Custom MediaType as MediaType.APPLICATION_JSON MediaType.APPLICATION_JSON和MediaType.APPLICATION_XML异常,但MediaType.TEXT_XML运行正常 - Exception with MediaType.APPLICATION_JSON and MediaType.APPLICATION_XML but MediaType.TEXT_XML running fine Rest api,添加另一个MediaType,默认响应 - Rest api, adding another MediaType, default response REST的MediaType - MediaType of REST @Produces(MediaType.APPLICATION_JSON) 和 @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 参数类型的内部详细信息? - Internal details for @Produces(MediaType.APPLICATION_JSON) and @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) argument types? Jersey演示应用程序中的MediaType.APPLICATION_XML和MediaType.APPLICATION_JSON - MediaType.APPLICATION_XML and MediaType.APPLICATION_JSON in a Jersey demo application 为什么没有像MediaType.APPLICATION_XML_UTF8这样的Spring MediaType? - Why isn't there a Spring MediaType like MediaType.APPLICATION_XML_UTF8? 向其他客户端使用的 REST api 端点添加另一个 MediaType - Adding another MediaType to REST api endpoint that is used by other clients 为什么我不能在 @RequestMapping 中使用 MediaType.APPLICATION_XML - Why can't I use MediaType.APPLICATION_XML in a @RequestMapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM