jsonpostjax-rsjsonprest-client

Create JAX-RS Client Post Request with JSON String as Body


I am writing a REST Client for one of the Vendor REST Service. I use jersey 2.x and JSON-P, below are dependencies I add.

<dependencies>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>
</dependencies>

I successfully write code for GET request and received JSON output. I saved it to a file and used JSON-P to interpret and do my own logic without any issues.

But now I need to write a POST request. When I use CURL as below I am able do it. and want to implement the same using Jeresey 2.x and JSON-P.

curl -v -H "Accept:application/json" -H "Content-Type:application/json" -u user:password -X POST --databinary @./InputParameters.json https://<IP>:<PORT>/Configuration/server

InputParameters.json contents
{
"ip": "10.197.70.16",
"partNumber": 202067,
"model": "IBM P7"
}

When I tried to pass response body as String in JSON format ({"ip": "10.197.70.16", "partNumber": 202067, "model": "IBM P7"}), but didn't work. So tried as JsonObject as below still didn't work.

JsonObject jsonObj = Json.createObjectBuilder()
            .add("ip", "10.197.70.16")
            .add("partNumber", 202067)
            .add("model", "IBM P7")
            .build();

response = invocationBuilder.post(Entity.json(jsonObj));

I know core java, based on that experience I jumped into writing this program and got success with GET but not POST. I doubt I am doing something fundamentally wrong with POST.


Solution

  • Issue is resolved after adding below dependency. At this point I am not sure on what does it do.

    Thanks to Swamy (TCS) for his support to resolve this.

    <dependency>
     <groupId>com.owlike</groupId>
     <artifactId>genson</artifactId>
     <version>1.4</version>
    </dependency>