gpt4 book ai didi

java - Spring @RequestBody 与 GeoJsonPoint

转载 作者:行者123 更新时间:2023-12-02 02:23:51 26 4
gpt4 key购买 nike

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

REST Controller :

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

类(class):

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 boot 应用程序是:

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

有人可以协助或提供任何指导吗?

最佳答案

我重新创建了整个场景,当 postman 调用电话时,GET 和 POST 都对我有用。您可以检查一下在服务器启动过程中是否收到以下行吗?

2018-09-03 15:10:41.995 INFO 17536 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping :映射“{[/location],methods=[POST],consumes=[application/json]}”到公共(public)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 中,它的作用就像一个魅力。如果您遇到任何问题,请告诉我。

关于java - Spring @RequestBody 与 GeoJsonPoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com