Spring @RequestBody with GeoJsonPoint

时间:2018-01-04 04:36:54

标签: java spring spring-boot

尝试将GeoJsonPoint JSON的POST请求映射到GeoJsonPoint模型时没有成功。

REST控制器:

@RestController
@RequestMapping("/location")
public class LocationController {

    @PostMapping(consumes = "application/json")
    public MyLocation recieveAddress(@RequestBody MyLocation location) {
        return location;
    }

    @GetMapping(consumes = "application/json")
    public MyLocation returnAddress() {
        MyLocation loc = new MyLocation();
        loc.setLocation(new GeoJsonPoint(0.123321, 0.3453455));
        return loc;
    }
}

类别:

public class MyLocation {

    private GeoJsonPoint location;

    public MyLocation() {   
    }

    public void setLocation(GeoJsonPoint location) {
        this.location = location;
    }

    public GeoJsonPoint getLocation() {
        return this.location;
    }

}

GET请求返回:

{
    "location": {
        "x": 0.123321,
        "y": 0.3453455,
        "type": "Point",
        "coordinates": [
            0.123321,
            0.3453455
        ]
    }
}

在通过调试发布上述内容时,我得到了:

DEBUG 11860 --- [nio-8090-exec-2] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /location 
DEBUG 11860 --- [nio-8090-exec-2] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/location]

寻找解决方案我偶然发现,没有运气:

@Configuration
public class JacksonConfig {
    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
        b.modulesToInstall(new GeoJsonModule());
        return b;
    }
}

Spring启动应用程序是:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

有人可以协助或给出任何指示吗?

2 个答案:

答案 0 :(得分:2)

我重新创建了整个场景,从邮递员拨打电话时,GET和POST对我都有效。您可以检查服务器启动期间是否收到以下行吗?

2018-09-03 15:10:41.995信息17536 --- [main] swsmmaRequestMappingHandlerMapping:将“ {[/ location],methods = [POST],consumes = [application / json]}映射到公共com .example.demo.location.MyLocation com.example.demo.location.LocationController.recieveAddress(com.example.demo.location.MyLocation)

如果您得到这一行,则意味着您的方法已准备好被访问。尝试使用以下配置与Postman通话:https://pasteboard.co/HCba9qa.png

确保发布的数据如下所示:

{
    "location": {
        "type": "Point",
        "coordinates": [
            0.123321,
            0.3453455
        ]
    }
}

因为不需要发送X和Y坐标。我已经使用MongoRepository在MongoDB中保存了此数据,它的工作原理就像一个超级按钮。让我知道您是否遇到任何问题。

答案 1 :(得分:0)

以下是正确映射端点的示例。我怀疑你的问题是以下任何一个

  1. 您的传入请求正文与My Location对象和GeoJsonPoint对象不匹配,因此,Spring无法将其与端点匹配
  2. 您可能遇到与Cross Origin
  3. 相关的问题
  4. 您使用的JSON序列化程序/反序列化程序未按预期工作
  5. 您的控制器设置不正确
  6. 以下适用于版本为Spring Boot的{​​{1}}项目,其中包含Spring Boot的默认1.5.3序列化程序/反序列化程序。

    示例JSON

    MyLocation

    示例package com.sample.controller; public class MyLocation { private CustomGeoJsonPoint location; //eclipse generated getters and setters }

    CustomGeoJsonPoint

    示例package com.sample.controller; import java.util.List; public class CustomGeoJsonPoint { private Double x; private Double y; private String type; private List<Double> coordiantes; //eclipse generated getters and setters }

    Controller

    package com.sample.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @CrossOrigin @RestController public class LocationController { @PostMapping("/location") public ResponseEntity<?> create(@RequestBody MyLocation inDTO) { System.out.println("Mapped successfully!"); return null; } } 使用Postman - &gt;提出的POST请求Body - &gt; raw

    JSON (application/json)

    与您不同,我不需要添加{ "x": 12.33456, "y": 33.33331, "type": "Point", "coordinates": [15.1234, 19.3322, 77.1234] }