java - 休息服务: select an appropriate method

标签 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).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/

相关文章:

java - Maven 程序集 : NoClassDefFoundError

java - Spring MVC 3 模糊映射

web-services - 添加 ws-security 以在 Mule 中流动

java - 返回 javax.ws.rs.core.Response 的列表,结果为 500

java - 泽西嵌入@Path

java - 如何向已注册的用户名发送对话框

java - 使用参数调用 RESTful webservice

c# - 网络服务跟踪/日志

rest - JAXB Jackson - 如何处理 map ?

java - Html Form无需提交自动调用Servlet