0

I have created a REST webservice which gives me 'GET JSON Response' as :

 {
        "payload": {
            "RFID": "E2005180040F003122202E5F",
            "chassisNumber": "4654689761",
            "vehicleNumber": "TN 01 1991"
        },
        "success": "true"
    }

Now I want Post Response from below Post Request :

Vehicle tag Request
{
  "vehicle_no": "TN07B0054"
 }

I have created the post method but it takes the whole thing as argument.

How to take vehicle argument as "TN07B0054" only from the Vehicle tag request.

Below is the POST response when I give above Vehicle Tag Request :

{
    "payload": {
        "vehicleNumber": "\"vehicle_no\": \"TN 07 B 0054\""
    },
    "success": "false"
}
Janhavi Gadkari
  • 93
  • 1
  • 12

2 Answers2

0

You can make a entity named VehicleTagRequest

public class VehicleTagRequest {
    private String vehicle_no;

    public String getVehicle_no() {
        return vehicle_no;
    }

    public void setVehicle_no(String vehicle_no) {
        this.vehicle_no = vehicle_no;
    }
}

Easiest way to deserialise your string to above java object is to use Jackson library (https://github.com/FasterXML/jackson)

If you are Maven for dependency management, you can add dependency like below in your pom.xml (this step is not required if you are maintaining your dependencies locally)

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0.pr4</version>
        </dependency>

Now comes deserialisation of your vehicle tag request json

Deserialisation using Jackson is done using ObjectMapper API if you are not using annotations, I have made a sample code snippet attached below-

public class VehicleSerialisation {

    public static void main(String[] args) {
        String vehicle = "{\"vehicle_no\": \"TN07B0054\"}";

        ObjectMapper objectMapper = new ObjectMapper();
        VehicleTagRequest vehicleTagRequest = null;
        try {
            vehicleTagRequest = objectMapper.readValue(vehicle, VehicleTagRequest.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print(vehicleTagRequest.getVehicle_no());
    }
}

You can then use the vehicleTagRequest.getVehicle_no() to form your request of GET JSON Response

Vishal
  • 588
  • 6
  • 24
  • Thank you so much for your time and help. I tried your answer but there was no success. I followed below link https://stackoverflow.com/questions/8194408/how-to-access-parameters-in-a-restful-post-method and it worked. – Janhavi Gadkari Jul 24 '17 at 10:19
0

This is not valid Json

{
    "payload": {
        "vehicleNumber": "\"vehicle_no\": \"TN 07 B 0054\""
    },
    "success": "false"
}

Without escape char

{
    "payload": {
        "vehicleNumber": "vehicle_no": "TN 07 B 0054"
    },
    "success": "false"
}

You can use like this

{
        "payload": {
            "vehicleNumber": {
          "vehicle_no": "TN 07 B 0054"
              }
        },
        "success": "false"
    }

And your POJO should be like

public class MyPojo
{
    private Payload payload;

    private String success;

    public Payload getPayload ()
    {
        return payload;
    }

    public void setPayload (Payload payload)
    {
        this.payload = payload;
    }

    public String getSuccess ()
    {
        return success;
    }

    public void setSuccess (String success)
    {
        this.success = success;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [payload = "+payload+", success = "+success+"]";
    }
}
vaquar khan
  • 8,163
  • 4
  • 54
  • 73