在下面的代码中选择适当的 Web 服务方法的逻辑是什么?
客户:
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> ";
}
}
为什么响应媒体类型将是 text/html
?定义它的规则是什么?例如,如果我需要 text/xml
作为响应怎么办?
最佳答案
我相信请求中的接受 header 定义了响应应采用的格式。有关详细信息,请参阅以下链接:http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
希望我的回答对你有帮助。
关于java - 休息服务: select an appropriate method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582152/