![](/img/trans.png)
[英]Can two rest resources have same methods and request mapping but different @path(url) in Restful service
[英]Two methods in Rest resource file with same @Path but different mediaType output
我的Java Rest资源文件中有2个方法,它们的@Path uri相同,但@produces不同。 下面的代码:
@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());
}
当我用POSTMAN rest client测试它时,我总是得到JSON输出! 有人可以解释为什么吗? 如果我想获取xml和json输出,该怎么办? 我尝试将内容类型更改为application / xml ..但是我总是得到json !!
Content-Type
用于发送的数据类型,由客户端作为请求标头或由服务器作为响应标头。 因此,作为客户端发送标头是没有用的,因为您没有发送任何数据。 对于客户端,当它想告诉服务器它想要什么类型时,它使用Accept: <media-type>
标头。
如果没有设置Accept
标头,则通常默认为*/*
,以便根据情况选择哪种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.