@RestController会自动填充请求体和标头。

huangapple go评论59阅读模式
英文:

RestController fill automatically request body and headers

问题

我们有RestController端点,可以在请求体和头部中接收参数。

因此,我们创建RequestVO对象,该对象会自动填充JSON(/XML)参数。

@PostMapping
public @ResponseBody ResponseEntity<String> endpoint(@RequestBody RequestVO requestVO, HttpServletRequest httpRequest) {

但是对于请求/头部参数,我们必须使用setter方法来填充值。

有没有一种方法可以自动填充对象的头部/请求参数呢?

英文:

We have RestController endpoints which receive parameters in body, and also from headers

So we create RequestVO object which is automatically filled with JSON(/XML) parameters

@PostMapping
public @ResponseBody ResponseEntity&lt;String&gt; endpoint(@RequestBody RequestVO requestVO, HttpServletRequest httpRequest) {

But for request/header's parameters we must use setters to fill the values

Is there a way to automatically fill object with headers/request parameters?

答案1

得分: 0

@RequestHeader(value="Key") String value,
@RequestHeader(value="Content-Type") String contentType,
...

英文:

if I understand you correctly, you can use something like this

@RequestHeader(value=&quot;Key&quot;) String value,
@RequestHeader(value=&quot;Content-Type&quot;) String contentType,

...

huangapple
  • 本文由 发表于 2020年9月30日 20:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64137301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定