简体   繁体   中英

How to pass JSON object to server from RESTful client in java

I'm creating a web application. I want to do the admin operations using Java SE application. To do that I created a RESTful ShoppingAdminClient in my SE project. I need to pass an JSON object to the server from client. I tried with this,

 public static void main(String[] args) throws JSONException {
    ShoppingAdminClient sac = new ShoppingAdminClient();
    JSONObject jo = new JSONObject();
    jo.put("itemName", "Itemms");
    sac.create_JSON(jo);

}

But I got the following exception,

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class shoppingadmin.Item, and MIME media type, application/json, was not found
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:149)
at com.sun.jersey.api.client.Client.handle(Client.java:648)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:563)
at shoppingadmin.ShoppingAdminClient.create_JSON(ShoppingAdminClient.java:69)
at shoppingadmin.ShoppingAdmin.main(ShoppingAdmin.java:27)
    Caused by: com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class shoppingadmin.Item, and MIME media type, application/json, was not found
at             com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204)
at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
... 6 more
Java Result: 1

How do I pass a JSON type object to server?

Thanks in Advance!

For Jersey client, you can create a Jersey Client instance to post your JSON to the server. So in order to send in JSON format, you can use Jackson JSON library and it can be bundled with Jersey by adding Jersey-JSON jars. You also need to add Jersey client dependency.

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.17</version>
    </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.17</version>
        </dependency>

Then your client code will look like:

public String requestResource(String baseURL, String path) {
        Client wsClient = Client.create();
        WebResource rs = wsClient.resource(baseURL);
        String response = rs.path(path).type(MediaType.APPLICATION_JSON).post(String.class, item);
        return response;

    }

Note that MediaType.APPLICATION_JSON in the type method specifies the data format. Here item contains the data you want to send to the server. Also, don't forget to put the following line in the web.xml so that all POJO classes will be automatically serialized to JSON.

        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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