在 Jersey Java 中发送和接收 JSON 到 REST WebService
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19050628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send and receive JSON to REST WebService in Jersey Java
提问by user2823345
I am new to Jersey Java REST WebService framework. I am trying to write a service method which consumes and produces JSON. My service code is below. It is simplest code, just for studying purpose.
我是 Jersey Java REST WebService 框架的新手。我正在尝试编写一个使用和生成 JSON 的服务方法。我的服务代码如下。这是最简单的代码,仅用于学习目的。
@Path("/myresource")
public class MyResource {
@Path("/sendReceiveJson")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String sendReceiveJson(String name)
{
System.out.println("Value in name: " + name);
return "{\"serviceName\": \"Mr.Server\"}";
}
}
And following is JerseyClient code.
以下是 JerseyClient 代码。
public class Program {
public static void main(String[] args) throws Exception{
String urlString="http://localhost:8080/MyWebService/webresources/myresource/sendReceiveJson";
URL url=new URL(urlString);
URLConnection connection=url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("{\"clientName\": \"Mr.Client\"}");
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}
}
But when i run service and then client, i am unable to send/receive JSON data. I get Exception at connection.getInputStream()
which is
但是当我运行服务然后运行客户端时,我无法发送/接收 JSON 数据。我得到异常connection.getInputStream()
是
Server returned HTTP response code: 405 for URL: http://localhost:8080/hellointernet/webresources/myresource/sendReceiveJson
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1625)
Please guide me, what needs to correct, or whether i am in wrong direction.
请指导我,有什么需要纠正的,或者我是否走错了方向。
采纳答案by TheArchitect
Your resource method is annotated as @GET which means any input data would have to be query string parameters.
您的资源方法被注释为@GET,这意味着任何输入数据都必须是查询字符串参数。
In this context @Consumes(MediaType.APPLICATION_JSON) doesn't make a lot of sense as only APPLICATION_FORM_URLENCODED is supported via GET.
在这种情况下,@Consumes(MediaType.APPLICATION_JSON) 没有多大意义,因为通过 GET 仅支持 APPLICATION_FORM_URLENCODED。
When you client calls setDoOutput(true) it probably switches your HTTP call to a POST hence causing the 405 Method Not Allowed.
当您的客户端调用 setDoOutput(true) 时,它可能会将您的 HTTP 调用切换到 POST,从而导致 405 Method Not Allowed。
If you want to consume JSON you should change your @GET annotation with @POST instead. Your client call should then work if it's indeed a POST. You can specify it with the following method:
如果您想使用 JSON,您应该使用 @POST 更改您的 @GET 注释。如果确实是 POST,则您的客户端调用应该可以工作。您可以使用以下方法指定它:
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
This API is pretty low level though, so I'd highly recommend you use Jersey's Client API instead. See https://jersey.java.net/documentation/1.17/client-api.html
不过这个 API 是相当低级的,所以我强烈建议你改用 Jersey 的客户端 API。见https://jersey.java.net/documentation/1.17/client-api.html