英文:
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<String, String> getHeaders(@Valid @RequestHeader Map<String, String> 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("/")
public String hello() {
Map<String, String> headers = myHeaders.getHeaders();
// Do something with the headers
return "Hello";
}
}
答案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=<your_value>
header.authorization.dataType=string
And next you can use @Value annotation in your controller class like below:
@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");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论