简体   繁体   English

Rest资源文件中的两种方法,它们的@Path相同,但mediaType输出不同

[英]Two methods in Rest resource file with same @Path but different mediaType output

I have 2 methods in my Java Rest resource file with same @Path uri but different @produces. 我的Java Rest资源文件中有2个方法,它们的@Path uri相同,但@produces不同。 The code below : 下面的代码:

@GET
@Path("/messages")
@Produces(MediaType.APPLICATION_XML)
 public List<Message> getAllMessages() {


    return new ArrayList<Message>(service.getMessageMap().values());
}

@GET
@Path("/messages")
@Produces(MediaType.APPLICATION_JSON)
 public List<Message> getAllMessagesJSON() {


    return new ArrayList<Message>(service.getMessageMap().values());
}

when i test it with POSTMAN rest client i always get JSON output!! 当我用POSTMAN rest client测试它时,我总是得到JSON输出! Can some one explain why?? 有人可以解释为什么吗? And if i want to get xml as well as json outputs, what to do?? 如果我想获取xml和json输出,该怎么办? I tried changing the content-type to application/xml..but i always get json!! 我尝试将内容类型更改为application / xml ..但是我总是得到json !!

Content-Type is for the type of data being sent, either by the client as a request header or by the server as a response header. Content-Type用于发送的数据类型,由客户端作为请求标头或由服务器作为响应标头。 So you as a client sending the header is useless, as you are not sending any data. 因此,作为客户端发送标头是没有用的,因为您没有发送任何数据。 For the client, when it want to tell the server what type it wants, it uses the Accept: <media-type> header. 对于客户端,当它想告诉服务器它想要什么类型时,它使用Accept: <media-type>标头。

When there is no Accept header set, it usually defaults to */* leaving it up for grabs which method to pick in your case. 如果没有设置Accept标头,则通常默认为*/* ,以便根据情况选择哪种方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM