1

I have two POJO classes.

Class 1 is Engine.java:

private String engineId;

public String getEngineId(){
  return this.engineId;
}   

public void setEngineId(String engineId){
  this.engineId = engineId;
}

The Second POJO class is Vehicle.java:

private String type;
private String manufacturer;
private Engine engine;

public String getType() {
   return type;
} 

public void setType(String type) {
    this.type = type;
} 

public String getManufacturer() {
     return manufacturer;
} 

public void setManufacturer(String manufacturer) {
     this.manufacturer = manufacturer;
}  

public Engine getEngine() {
     return engine;
} 

public void setEngine(Engine engine) {
    this.engine = engine;
}

I have a REST Controller for providing information of Vechicles (RequestMethod.GET):

@RequestMapping(method = RequestMethod.GET)
public Vehicle getVechileDetails(Vehicle inputVehicle){
   Vehicle vehicle = new Vehicle();
    // some processing
    return vehicle;
}

When I hit this service and provide the Request parameter as type or manufacturer, then the Spring creates the Vehicle object and populates the value of type and manufacturer. But if I provide the value of engineId, then the Spring is not able to create the Engine object such that vehicle.getEngine().getEngineId() != null

Is there any way in which if I invoke my Rest Service like:

http://localhost:8080/Controller/getVehicleDetails?engineId=12345

then the Vehicle is created with Engine having the value of engineId ?

arghtype
  • 4,376
  • 11
  • 45
  • 60
Rahul
  • 198
  • 4
  • 10

2 Answers2

1

You can get the vehicleId like this (ResponseEntity structure is included):

    @RequestMapping(value = "/Controller/getVehicleDetails", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public
    @ResponseBody
    ResponseEntity<AjaxResponse> controller(@RequestParam(value = "engineId") Long engineId) {
        //Do whatever you want with the engineId
        return new ResponseEntity<AjaxResponse>(new AjaxResponse("Success"), HttpStatus.OK);
    }

But, for POJOs, I have to warn two things:

  1. Make sure your classes implement Serializable
  2. For the fields which are not certain while converting from Java to Javascript or vice versa, like Date variables, You have to set your JsonSerializer class properly

Update: You wanted to get the object from request, so:

    @RequestMapping(value = "/Controller/getVehicleDetails", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, , consumes = MediaType.APPLICATION_JSON_VALUE)
    public
    @ResponseBody
    ResponseEntity<AjaxResponse> controller(@RequestBody Vehicle vehicle) {
        //vehicle.getEngine().getEngineId()
        return new ResponseEntity<AjaxResponse>(new AjaxResponse("Success"), HttpStatus.OK);
    }

POJOS:

    public class Engine implements Serializable {
        //...
    }

    public class Vehicle implements Serializable {
        private Engine engine;
    }

JS side:

    var vehicle = {
        engine: {id: 123}//,
        //...
    }
    //via angularjs:
    $http.post("http://localhost:8080/Controller/getVehicleDetails", vehicle).success(function(response) {
        console.log("success");
    });
Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61
  • The problem is not how can i get engineId as an Input Parameter. The problem is I want Vehicle Object as the Input Parameter, even if the user hits the service by passing the engineId. – Rahul Feb 06 '17 at 08:58
  • Do you want to get it as object from the request? – Bahadir Tasdemir Feb 06 '17 at 09:26
  • Yes. I want the Vehicle object populated with the engineId, such that : `vehicle.getEngine().getEngineId()` should give me the value. – Rahul Feb 06 '17 at 09:28
  • you changed the RequetMethod from GET to POST. :) Need the Vehicle object as an input parameter where the request type is GET. – Rahul Feb 06 '17 at 10:45
  • If you are posting an object at the request body, you have to set it to POST – Bahadir Tasdemir Feb 06 '17 at 11:52
  • I understand what you are saying. So it means there is no way in which we can send the Object with Request.GET method ? – Rahul Feb 07 '17 at 01:57
  • Yes, please read this naswer: http://stackoverflow.com/a/13270939/1201725 and don't forget to mark my answer as the correct one – Bahadir Tasdemir Feb 07 '17 at 07:23
  • Thank you. The link you have provided helps ! – Rahul Feb 07 '17 at 09:01
0

Here you can refer , where the request body has JSON which converted to Object See : For references

@RequestMapping(value = EmpRestURIConstants.CREATE_EMP, method = RequestMethod.POST)
    public @ResponseBody Employee createEmployee(@RequestBody Employee emp) {
        logger.info("Start createEmployee.");
        emp.setCreatedDate(new Date());
        empData.put(emp.getId(), emp);
        return emp;
    }