如何在Spring Boot中使用 @Valid @RequestHeader Map<String, String> 头部

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

How to use @Valid @RequestHeader Map<String, String> headers in spring boot

问题

@Valid注解用于Spring Boot中的@RequestHeader Map<String, String>字段的示例和建议。

英文:

Can someone please suggest and provide example on how to use the @Valid annotation for @RequestHeader Map<String, String> fields in spring boot

答案1

得分: 1

以下是翻译好的代码部分:

你可以编写类似下面的类

    import org.springframework.validation.annotation.Validated;
    import javax.validation.Valid;
    import java.util.Map;

    @Validated
    public class MyHeaders {
    public Map<String, String> getHeaders(@Valid @RequestHeader Map<String, String> headers) {
    return headers;
    }
    }

下面是我的控制器类

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;

    private final MyHeaders myHeaders;

    public MyController(MyHeaders myHeaders) {
    this.myHeaders = myHeaders;
    }
    @GetMapping("/")
    public String hello() {
        Map<String, String> headers = myHeaders.getHeaders();
        // 对头部进行一些操作
        return "Hello";
    }
    }
英文:

You can write class like below:

import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import java.util.Map;

@Validated
public class MyHeaders {
public Map&lt;String, String&gt; getHeaders(@Valid @RequestHeader Map&lt;String, String&gt; headers) {
return headers;
}
}

below is my controller class:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

private final MyHeaders myHeaders;

public MyController(MyHeaders myHeaders) {
this.myHeaders = myHeaders;
}
@GetMapping(&quot;/&quot;)
public String hello() {
    Map&lt;String, String&gt; headers = myHeaders.getHeaders();
    // Do something with the headers
    return &quot;Hello&quot;;
}
}

答案2

得分: 0

是的,我们可以从消息属性列表中传递值和名称。
首先,您可以在“application.properties”文件中定义消息属性。

header.authorization.name=Authorization
header.authorization.value=<your_value>
header.authorization.dataType=string

接下来,您可以在您的控制器类中使用@Value注解,如下所示:

@RestController
public class HelloWorld{
    
    @Value("${header.authorization.name}")
    private String authorizationHeaderName;

    @Value("${header.authorization.value}")
    private String authorizationHeaderValue;

    @Value("${header.authorization.dataType}")
    private String authorizationHeaderDataType;

    @PostMapping(value = "/getmessage")
    @ApiImplicitParams({@ApiImplicitParam(name = "${header.authorization.name}", value = "${header.authorization.value}", dataType = "${header.authorization.dataType}", paramType = "header")})
    public ResponseEntity<?> getMessage(){
        
        return ResponseEntity.ok("HelloWorld");

    }
    
}

这将允许您从application.properties文件中读取消息属性的值,并将其注入到控制器类中的相应字段中。

英文:

Yes, we can pass the value and name from list of message property.
First you can define the message properties in "application.properties" file.

header.authorization.name=Authorization
header.authorization.value=&lt;your_value&gt;
header.authorization.dataType=string

And next you can use @Value annotation in your controller class like below:

@RestController
public class HelloWorld{
    
    @Value(&quot;${header.authorization.name}&quot;)
    private String authorizationHeaderName;

    @Value(&quot;${header.authorization.value}&quot;)
    private String authorizationHeaderValue;

    @Value(&quot;${header.authorization.dataType}&quot;)
    private String authorizationHeaderdataType;

    @PostMapping(value = &quot;/getmessage&quot;)
    @ApiImplicitParams({@ApiImplicitParam(name = ${header.authorization.name}&quot;, value = &quot;${header.authorization.value}&quot;, dataType = 
 ${header.authorization.dataType}&quot;, paramType = &quot;header&quot;)
    })
    public responseEntity&lt;?&gt; getMessage(){
        
        return ResponseEntity.ok(&quot;HelloWorld&quot;);

    }
    
}

huangapple
  • 本文由 发表于 2023年2月16日 16:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469538.html
匿名

发表评论

匿名网友

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

确定