Java RESTWeb服务使用CodenameOne客户端始终返回html
我已经通过jersey和tomcat创建了一个web服务,如下所示:Java RESTWeb服务使用CodenameOne客户端始终返回html,java,rest,tomcat,jersey,codenameone,Java,Rest,Tomcat,Jersey,Codenameone,我已经通过jersey和tomcat创建了一个web服务,如下所示: @Path("/hello") public class Hello { // This method is called if TEXT_PLAIN is request @GET @Produces(MediaType.TEXT_PLAIN) public String sayPlainTextHello() { boolean flag=true; return "Hello
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
boolean flag=true;
return "Hello Jersey";
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
boolean flag=true;
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() {
boolean flag=true;
return "<html> " + "<title>" + "Hello Jersey" + "</title>"
+ "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
}
codenameone中的默认示例设置了req.setContentType(“text/plain”);但是在客户端和ws中更改此选项的结果是相同的,我总是得到html,而不是xml或文本…这并不重要,因为我很快就会得到json,但我想我必须在继续之前理解这一点…谢谢你案例1:
如果要基于接受类型标头调用,
更改text/html等的accept头以调用适当的方法
接受标题
:text/plain
调用sayplantextHello()
案例2:
如果希望仅基于URI处理调用调用:
请注意,来自客户端的URI:
是根URI,而不是方法级URI。如果您有这样的方法:
public static void getRest(){
String service="http://localhost:8080/com.vogella.jersey.first/rest/hello";
ConnectionRequest req = new ConnectionRequest(){
@Override
protected void postResponse() {
}
@Override
protected void readResponse(InputStream input) throws IOException {
// JSONParser p = new JSONParser();
// System.out.println(Util.readToString(input));
Dialog dd=new Dialog(Util.readToString(input));
hi.addComponent(dd);
}
};
req.setUrl(service);
req.setPost(false);
req.setContentType(MediaType.TEXT_PLAIN);
InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
req.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueue(req);
}
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/text")
public String sayPlainTextHello() {
//do something
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
@Path("/xml")
public String sayXMLHello() {
//do something
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/html")
public String sayHtmlHello() {
//do something
}
如您所见,您现在有了方法级别@Path
,然后可以调用
我刚刚给出了一个示例,典型的实现可以基于QueryParams,响应类型为
案例1:
如果要基于接受类型标头调用,
更改text/html等的accept头以调用适当的方法
接受标题
:text/plain
调用sayplantextHello()
案例2:
如果希望仅基于URI处理调用调用:
请注意,来自客户端的URI:
是根URI,而不是方法级URI。如果您有这样的方法:
public static void getRest(){
String service="http://localhost:8080/com.vogella.jersey.first/rest/hello";
ConnectionRequest req = new ConnectionRequest(){
@Override
protected void postResponse() {
}
@Override
protected void readResponse(InputStream input) throws IOException {
// JSONParser p = new JSONParser();
// System.out.println(Util.readToString(input));
Dialog dd=new Dialog(Util.readToString(input));
hi.addComponent(dd);
}
};
req.setUrl(service);
req.setPost(false);
req.setContentType(MediaType.TEXT_PLAIN);
InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
req.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueue(req);
}
@Path("/hello")
public class Hello {
// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("/text")
public String sayPlainTextHello() {
//do something
}
// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
@Path("/xml")
public String sayXMLHello() {
//do something
}
// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
@Path("/html")
public String sayHtmlHello() {
//do something
}
如您所见,您现在有了方法级别@Path
,然后可以调用
我刚刚给出了一个示例,典型的实现可以基于QueryParams,响应类型为
我从未使用过CodeNameOne,但我非常确定
setContentType
设置了Content-Type
标题。在请求中,也就是说客户端发送的数据类型就是该内容类型。但是GET通常没有数据,因此内容类型
是无用的
对于请求,当您想要告诉服务器您想要返回的内容类型时,您可以设置Accept
标题。当您不设置它时,它通常默认为*/*
,这使得Jersey只能选择一个。在您的情况下,它似乎总是选择text/html
看起来,我没有看到任何setAccept
方法,但它看起来可以使用。索特
我从未使用过CodeNameOne,但我非常确定
setContentType
设置Content-Type
标题。在请求中,也就是说客户端发送的数据类型就是该内容类型。但是GET通常没有数据,因此内容类型
是无用的
对于请求,当您想要告诉服务器您想要返回的内容类型时,您可以设置Accept
标题。当您不设置它时,它通常默认为*/*
,这使得Jersey只能选择一个。在您的情况下,它似乎总是选择text/html
看起来,我没有看到任何setAccept
方法,但它看起来可以使用。索特
我将完全删除案例2。这种内容协商不应该在这样的url中进行,除非在极少数情况下您使用像
.json
@peeskillet这样的“文件”扩展名,我完全同意。我给出了案例2,以防有东西通过Accept强制无法处理。我将完全删除案例2。这种内容协商不应该在这样的url中进行,除非在极少数情况下您使用像.json
@peeskillet这样的“文件”扩展名,我完全同意。我给出了第二种情况,以防有什么事情通过Accept无法处理。