尝试将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);
    }
}


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

最佳答案

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

2018-09-03 15:10:41.995 INFO 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中保存了此数据,它的工作原理就像一个超级按钮。如果您遇到任何问题,请告诉我。

09-16 06:55