Java REST服务:选择适当的方法
对于下一个代码,选择合适的web服务方法的逻辑是什么 客户:Java REST服务:选择适当的方法,java,web-services,rest,jax-rs,jersey-2.0,Java,Web Services,Rest,Jax Rs,Jersey 2.0,对于下一个代码,选择合适的web服务方法的逻辑是什么 客户: HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin"); final Client client = ClientBuilder.newClient(); client.register(feature); final Response response = client.target(webServiceURI).re
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin");
final Client client = ClientBuilder.newClient();
client.register(feature);
final Response response = client.target(webServiceURI).request().get();
System.out.println(response.getMediaType());
服务:
@Path("/helloworld")
public class HelloWorld {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello World RESTful Jersey!";
}
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "<?xml version=\"1.0\"?>" + "<hello> Hello World RESTful Jersey"
+ "</hello>";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "<html> " + "<title>" + "Hello World RESTful Jersey"
+ "</title>" + "<body><h1>" + "Hello World RESTful Jersey"
+ "</body></h1>" + "</html> ";
}
}
@Path(“/helloworld”)
公共类HelloWorld{
@得到
@生成(MediaType.TEXT\u PLAIN)
公共字符串sayplantextHello(){
返回“Hello World RESTful Jersey!”;
}
@得到
@生成(MediaType.TEXT\u XML)
公共字符串sayXMLHello(){
返回“+”Hello World RESTful Jersey”
+ "";
}
@得到
@生成(MediaType.TEXT\u HTML)
公共字符串sayHtmlHello(){
返回“+”+“Hello World RESTful Jersey”
+“+”+“你好,世界宁静运动衫”
+ "" + " ";
}
}
为什么响应媒体类型将为
text/html
?定义它的规则是什么?例如,如果响应中需要text/xml
,该怎么办?我认为是请求中的accept头定义了响应应采用的格式。有关详细信息,请参阅以下链接:
我希望答案能有所帮助。我认为是请求中的accept头定义了响应应采用的格式。有关详细信息,请参阅以下链接:
我希望答案能有所帮助。您的容器可能会在请求中使用Accept头来确定要调用三个方法中的哪一个。如果请求在Accept标头中仅包含text/plain,它将调用返回text/plain等的方法。此处内容协商文档中的更多信息:您的容器可能会在请求中使用Accept标头来确定要调用三种方法中的哪一种。如果请求在Accept头中只包含text/plain,它将调用返回text/plain等的请求。更多信息请参见此处的内容协商文档:
.request(MediaType.text\u XML)。get()
或请求().Accept(MediaType.text\u XML)。get()
请求(MediaType.text\u XML)。get()
或请求().Accept(MediaType.TEXT\u XML).get()