Java @Pathvariable到classDTO
是否可以不初始化类的所有字段,而直接创建类pathvariable 我知道我可以使用POST请求,但在这种情况下,我需要GET请求 我想要这个 @GetMapping(“/get/{classDTO}”) 公共字符串getMethod(@PathVariable classDTO classDTO) 而不是这个 @GetMapping(“/get”) 公共字符串getMethod(@PathVariable String id、@PathVariable String random、@PathVariable String variable、@PathVariable String hello)直接在URI中传递多个值(例如对象)非常容易出错。 然而,有这样做的可能性。即使我不会在实际应用中建议它。此外,请确保您没有传递任何敏感数据 假设您有一本类“书”: 您的控制器如下所示:Java @Pathvariable到classDTO,java,spring,http,request,Java,Spring,Http,Request,是否可以不初始化类的所有字段,而直接创建类pathvariable 我知道我可以使用POST请求,但在这种情况下,我需要GET请求 我想要这个 @GetMapping(“/get/{classDTO}”) 公共字符串getMethod(@PathVariable classDTO classDTO) 而不是这个 @GetMapping(“/get”) 公共字符串getMethod(@PathVariable String id、@PathVariable String random、@PathV
@RestController
public class LibraryController {
@GetMapping("/library")
public Book getBook(@RequestParam String book) throws JsonProcessingException {
return new ObjectMapper().readValue(book, Book.class);
}
}
发生的情况是,我们正在手动尝试将传递的字符串映射到预期的对象
我们的get请求如下:
http://localhost:8080/library?book={"name": "title", "author": "someone", "pages": 400}
额外资源:
在互联网上搜索答案的时候,我发现了两篇你可能感兴趣的文章
对于这种情况,我刚刚编写了一个自定义编码器。。。但后来我意识到我可以使用@RequestParam。。。
http://localhost:8080/library?book={"name": "title", "author": "someone", "pages": 400}