Reputation:
I am trying to build a spring boot @PostMapping method that gets it's parameters from the uri, something like this http://localhost:8091/url/log?param1=asdf¶m2=asd¶m3=test or like this http://localhost:8091/url/log/msg/msg1/msg2
Is there a way to expand the code model from below to 3 parameters? 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);
}
Upvotes: 0
Views: 9157
Reputation: 66
If you simply want your API call to create a new Article, how about simply having a PostMapping
to /article/new
(for example) and then simply pass the new Article's parameters as RequestBody?
@PostMapping("article/new")
public ResponseEntity<Void> addArticle(@RequestBody Article article) {
// ...
}
Then as RequestBody you would have something like:
{ "param1": "value1", "param2": "value2", "param3": "value3" }
If you're just looking to include more PathVariable's to your api call, refer to @sovannarith cheav's answer
I hope this helps
Upvotes: 5
Reputation: 793
Is there a way to expand the code model from below to 3 parameters? headers.setLocation(builder.path("/article/{param1}/{param2}/{param3}")
You can use @PathVariable, like below
@PostMapping("article/{param1}/{param2}/{param3}")
public ResponseEntity<Void> addArticle(@PathVariable("param1") String param1, @PathVariable("param3") String param3, @PathVariable("param3") String param3) {
//enter code here
}
Upvotes: 0