简体   繁体   English

在Jersey Java中发送和接收JSON到REST WebService

[英]Send and receive JSON to REST WebService in Jersey Java

I am new to Jersey Java REST WebService framework. 我是Jersey Java REST WebService框架的新手。 I am trying to write a service method which consumes and produces JSON. 我正在尝试编写使用并生成JSON的服务方法。 My service code is below. 我的服务代码如下。 It is simplest code, just for studying purpose. 这是最简单的代码,仅用于学习目的。

@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. 但是当我运行服务然后运行客户端时,我无法发送/接收JSON数据。 I get Exception at connection.getInputStream() which is 我在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. 请指导我,需要纠正什么,或者我的方向是否错误。

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. 如果要使用JSON,则应改为使用@POST更改@GET注释。 Your client call should then work if it's indeed a POST. 如果确实是POST,则您的客户呼叫应该可以工作。 You can specify it with the following method: 您可以使用以下方法指定它:

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. 不过,该API的级别很低,因此,我强烈建议您改用Jersey的Client API。 See https://jersey.java.net/documentation/1.17/client-api.html 参见https://jersey.java.net/documentation/1.17/client-api.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM