0

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!

begginerAll
  • 159
  • 7
  • 19
  • http://stackoverflow.com/questions/7832760/a-message-body-writer-for-java-type-class-mypackage-b-and-mime-media-type-app this link might help – Balaji Krishnan Dec 27 '13 at 16:27

1 Answers1

3

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>
tonga
  • 11,749
  • 25
  • 75
  • 96
  • Where do I set that dependencies? My client application is a Java SE Application. Thanks – begginerAll Dec 27 '13 at 16:37
  • And what are these `MyItem` and `item`? – begginerAll Dec 27 '13 at 16:45
  • Just add it in your pom.xml if you use Maven. Otherwise, you can download the jars manually. – tonga Dec 27 '13 at 16:46
  • `item` is the data you want to send to the server. It can be any POJO classes, e.g., `MyItem item = new MyItem();` where `MyItem` is the class you create for the data you want to send. It can be as simple as a map in your example `"itemName" -> "Itemms"`. – tonga Dec 27 '13 at 16:48
  • @tonga Can you explain what the `path`parameter should have. Is there a difference b/w `baseURL & path` – 09Q71AO534 Jan 02 '15 at 12:49
  • @09Q71AO534 The `path` parameter is the additional path in your URL after the baseURL, which is the web root context, such as www.example.com/myapp, if you set up your web app root context as `myapp`. So for example, www.example.com/myapp/item/123, the baseURL is www.example.com/myapp, and the path is /item/123. – tonga Jan 02 '15 at 15:59