【发布时间】:2019-10-18 09:25:01
【问题描述】:
我正在尝试构建一个从 uri 获取参数的 spring boot @PostMapping 方法,类似这样 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);
}
【问题讨论】:
-
首先:您的帖子调用应该有请求参数。
http://localhost:8091/url/log?param1=asdf&param2=asd&param3=test是一种不好的做法。第二:http://localhost:8091/url/log/msg/msg1/msg2是一个好方法更好的方法是创建一个 Request json 对象 -
问题是我在 ArticleRepository 之前没有使用@Autowired :(
-
我现在将使用 Sovannarith 方法,我太笨了。也许它会帮助其他人。
标签: java spring-boot