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!

1

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Where do I set that dependencies? My client application is a Java SE Application. Thanks
And what are these MyItem and item?
Just add it in your pom.xml if you use Maven. Otherwise, you can download the jars manually.
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 Can you explain what the pathparameter should have. Is there a difference b/w baseURL & path
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.