我正在尝试构建一个Spring Boot @PostMapping方法,该方法从uri获取它的参数,像这样 http://localhost:8091/url/log?param1=asdf¶m2=asd¶m3=test 或像这样 http://localhost:8091/url/log/msg/msg1/msg2
有没有办法将代码模型从下面扩展到3个参数? headers.setLocation(builder.path(“ / article / {param1} / {param2} / {param3}”)
@PostMapping("article")
public ResponseEntity<Void> addArticle(@RequestBody ArticleInfo articleInfo, UriComponentsBuilder builder) {
Article article = new Article();
BeanUtils.copyProperties(articleInfo, article);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/article/{id}").buildAndExpand(article.getArticleId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
}
答案 0 :(得分:1)
如果您只是想通过API调用来创建新的Article,那么如何简单地将PostMapping
设置为/article/new
(例如),然后将新Article的参数作为RequestBody传递呢? >
@PostMapping("article/new")
public ResponseEntity<Void> addArticle(@RequestBody Article article) {
// ...
}
然后作为RequestBody,您将拥有类似以下内容:
{ "param1": "value1", "param2": "value2", "param3": "value3" }
如果您只是想在API调用中包含更多PathVariable,请参阅@sovannarith cheav的答案
我希望这对您有帮助
答案 1 :(得分:-1)
有没有办法将代码模型从下面扩展到3个参数? headers.setLocation(builder.path(“ / article / {param1} / {param2} / {param3}”)
您可以像下面一样使用@PathVariable
@PostMapping("article/{param1}/{param2}/{param3}")
public ResponseEntity<Void> addArticle(@PathVariable("param1") String param1, @PathVariable("param3") String param3, @PathVariable("param3") String param3) {
//enter code here
}