将请求参数转换为Class对象

时间:2017-02-06 08:30:11

标签: java spring spring-rest

我有两个POJO课程。

第1类是 Engine.java

private String engineId;

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

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

第二个POJO类是 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;
}

我有一个REST控制器,用于提供Vechicles(RequestMethod.GET)的信息:

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

当我点击此服务并提供Request参数作为类型或制造商时,Spring会创建Vehicle对象并填充类型和制造商的值。但是如果我提供engineId的值,那么Spring就无法创建引擎对象vehicle.getEngine().getEngineId() != null

如果我调用我的Rest Service,有没有办法如下:

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

然后使用引擎创建Vehicle,其值为engineId?

2 个答案:

答案 0 :(得分:1)

您可以像这样获得vehicleId(包含ResponseEntity结构):

    @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);
    }

但是,对于POJO,我必须警告两件事:

  1. 确保您的课程实施Serializable
  2. 对于从Java转换为Javascript时不确定的字段,反之亦然,如Date变量,您必须正确设置JsonSerializer类
  3. 更新:您希望从请求中获取对象,因此:

        @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);
        }
    

    POJO。:

        public class Engine implements Serializable {
            //...
        }
    
        public class Vehicle implements Serializable {
            private Engine engine;
        }
    

    JS方面:

        var vehicle = {
            engine: {id: 123}//,
            //...
        }
        //via angularjs:
        $http.post("http://localhost:8080/Controller/getVehicleDetails", vehicle).success(function(response) {
            console.log("success");
        });
    

答案 1 :(得分:0)

在这里你可以参考,请求体具有转换为Object的JSON 请参阅: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;
    }