java 休息 | @Produces 和 @Consumes :为什么他们都没有被调用为相同的 MIME 类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17148529/
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
Rest | @Produces and @Consumes : why dont they both get called for same MIME-type
提问by arthur
a newbie in JAX-REST (jersey 1.8 impl)
JAX-REST 的新手(jersey 1.8 impl)
I have a class for Resource "/hello"
我有一个资源类“/hello”
package com.lbs.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Test {
//-- produces MIME type text/plain
@GET
@Produces(MediaType.TEXT_PLAIN)
public String thankYouTxt(){
System.out.println("thankYouTxt");
return "thankYouTxt\n";
}
//-- consumes MIME type text/plain
@GET
@Consumes(MediaType.TEXT_PLAIN)
public String thankYouInputTxt(){
System.out.println("thankYouInputTxt");
return "thankYouInputTxt";
}
//-- produces MIME type text/html
@GET
@Produces(MediaType.TEXT_HTML)
public String thankYouHTML(){
System.out.println("thankYouHTML");
return "thankYouTxtHTML";
}
//-- consumes MIME type text/html
@GET
@Consumes(MediaType.TEXT_HTML)
public void thankYouInputHTML(){
System.out.println("thankYouInputHTML");
//return "thankYouInputHTML";
}
//-- produces MIME type text/xml
@GET
@Produces(MediaType.TEXT_XML)
public String thankYouXML(){
System.out.println("thankYouXml");
return "<?xml version=\"1.0\"?> <message>thankYouTxt</message>";
}
//-- consumes MIME type text/xml
@GET
@Consumes(MediaType.TEXT_XML)
public String thankYouInputXML(){
System.out.println("thankYouInputXML");
return "thankYouInputXML";
}
}
when I sent a request with a header Content-Type : text/html
, I would expect both the @Produces
and @Consumes
annotated methods thankYouHTML()
and thankYouInputHTML()
to be called.
当我发送的请求有一个头Content-Type : text/html
,我希望无论是@Produces
和@Consumes
注解的方法thankYouHTML()
和thankYouInputHTML()
被调用。
but only the @Produces thankYouHTML()
method get called? why? why is not the @Consumes
annotated method thankYouHInputTML()
also called?
但只有@Produces thankYouHTML()
方法被调用?为什么?为什么不也调用带@Consumes
注释的方法thankYouHInputTML()
?
回答by pWoz
You should remember that:
你应该记住:
- Only one method is executed for a single request. So it is impossible to execute two methods (or more) in single request;
- JAX-RS runtime decides which one method should be executed according to request header values sent to to the server.
- 对于单个请求只执行一种方法。所以不可能在单个请求中执行两个(或多个)方法;
- JAX-RS 运行时根据发送到服务器的请求头值决定应该执行哪一种方法。
JAX-RS
runtime tries to match:
JAX-RS
运行时尝试匹配:
http method (
GET
,POST
, ...) with proper annotation (@GET
,@POST
,...);request path (
'/api/something'
) with the proper@Path
annotation;http
content-type
header (link) with proper@Consumes
annotation;http
accept
header with propper@Produces
annotation;
http 方法 (
GET
,POST
, ...) 带有适当的注释 (@GET
,@POST
,...);请求路径 (
'/api/something'
) 带有正确的@Path
注释;带有正确注释的http
content-type
标头(链接)@Consumes
;accept
带有正确@Produces
注释的http标头;
So (for example) @Produces
annotation does not denote that annotated method produces something. It denotes that method will be executed when matching accept header
will be contained in request.
所以(例如)@Produces
注解并不表示注解的方法会产生一些东西。它表示当匹配accept header
将包含在请求中时将执行该方法。
If You need more information abou RestFull webservices i advice You to read these resources:
如果您需要有关 RestFull 网络服务的更多信息,我建议您阅读以下资源: