将点对象传递给@RequestBody

时间:2017-08-15 10:21:57

标签: java spring-boot request geometry

我正在使用spring boot开发rest api服务。我的项目有一个名为CarWash的实体类,带有字段

import com.vividsolutions.jts.geom.Point;
@Column(columnDefinition = "Point", name = "lon_lat")
    private Point lonLat;
 @Column(name = "address")
private String address;

这是我的控制器方法

@RequestMapping(value = "owner/carWashs", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")


 @ResponseBody
    public ResponseEntity<String> createCarWash(@RequestParam(value = "sid", required = true, defaultValue = "") String sid,
                                               @RequestBody CarWash carWash) throws TimeoutException, SocketTimeoutException, SocketException {
        final Integer id = checkSession(sid);
        System.out.println(carWash.getLonLat());
        System.out.println(carWash.getAddress());
        return ResponseEntity.ok(createCarWashLogic(carWash));
    }

当我使用这个json {"address":"qwerty"}调用此方法时,它可以正常工作。 问题是如何将lonLat属性添加到请求体中以便jackson能够将其解析为POINT对象?

2 个答案:

答案 0 :(得分:0)

假设您有

class Point{ String attribute1; String attribute2; }

发送Carwash json,如

{ "address":"qwerty", "lonLat" : { "attribute1":"value", "attribute2":"value" } }

和杰克逊将相应地设置属性值。

答案 1 :(得分:0)

class的字段 CoordinateSequence坐标,该字段的字段为 int X,int Y,int Z,int M < / strong>即可。所以你的json字符串应该是这样的:

{
  "address": "some address",
  "lonLat": {
    "coordinates": {
      "X": 123,
      "Y": 123,
      "Z": 123,
      "M": 123,
    }
  }
}