0

我从泽西教程中学习了这个简单的课程。如何进行简单的测试,只需通过 curl 切换格式的返回类型?我不想写客户端。

@Path("/hello")
public class Hello {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>" + "<body><h1>" + "Hello Jersey" + "</body></h1>"
                + "</html> ";
    }

}
4

1 回答 1

1

对 XML 试试这个:

curl -H "Accept: text/xml" -H "Content-Type: text/xml" -X GET "http://localhost:8080/yourapp/hello"

与 text/html 和 text/plain 类似

于 2013-09-15T13:03:54.110 回答