@ModelAttributeSpring MVC 的目的和用途是什么?
我的REST控制器中有一些方法,它包含很多参数.例如:
@RequestMapping(value = "/getItem", method = RequestMethod.GET)
public ServiceRequest<List<SomeModel>> getClaimStatuses(
@RequestParam(value = "param1", required = true) List<String> param1,
@RequestParam(value = "param2", required = false) String param2,
@RequestParam(value = "param3", required = false) List<String> param3,
@RequestParam(value = "param4", required = false) List<String> param4,
@RequestParam(value = "param5", required = false) List<String> param5) {
// ......
}
Run Code Online (Sandbox Code Playgroud)
我想将所有GET请求参数映射到POJO对象,如:
public class RequestParamsModel {
public RequestParamsModel() {
}
public List<String> param1;
public String param2;
public List<String> param3;
public String param4;
public String param5;
}
Run Code Online (Sandbox Code Playgroud)
我需要在REST控制器中使用@RequestBody这样的东西. …
在Jersey中有一个@BeanParam注释,我可以将请求参数映射到bean属性.
在Spring中,我只能找到@RequestBody哪个显然适用于请求体而不是请求参数.
有没有办法让请求参数使用Spring映射到bean?
@JsonCreator 不反序列化枚举类型的 @RequestParam
我正在开发一个 Spring 应用程序,其中控制器正在接收 Spring 绑定到包装器对象的请求参数列表。其中一个参数是 enum 类型,我通过某个属性名称接收它。
Endpoint example: http://localhost:8080/searchCustomers?lastName=Smith&country=Netherlands
@RequestMapping(value = "/search/customers", method = RequestMethod.GET)
public CustomerList searchCustomers(@Valid CustomerSearchCriteria searchCriteria)
public class CustomerSearchCriteria {
private String lastName;
private Country country;
}
public enum Country {
GB("United Kingdom"),
NL("Netherlands")
private String countryName;
Country(String countryName) {
countryName = countryName;
}
@JsonCreator
public static Country fromCountryName(String countryName) {
for(Country country : Country.values()) {
if(country.getCountryName().equalsIgnoreCase(countryName)) {
return country;
}
}
return null;
}
@JsonValue
public String toCountryName() {
return countryName; …Run Code Online (Sandbox Code Playgroud) Spring Cloud openFeign 无法创建动态查询参数。它抛出以下异常,因为 SpringMvcContract 试图找到不存在的 RequestParam 值属性。
java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
@RequestMapping(method = RequestMethod.GET, value = "/orders")
Pageable<Order> searchOrder2(@RequestParam CustomObject customObject);
Run Code Online (Sandbox Code Playgroud)
我尝试使用@QueryMap而不是@RequestParam但@QueryMap不生成查询参数。
顺便说一句,@RequestParam Map<String, Object> params方法参数可以很好地生成动态查询参数。
但我想使用一个自定义对象,其中 feign 客户端可以从对象的属性生成动态查询参数。
spring spring-mvc spring-boot spring-cloud spring-cloud-feign
我使用 \xe3\x80\x8cpostman\xe3\x80\x8d 发送“application/json”类型的 POST 请求,将参数“phone”设置为空字符串,通常它应该打印注释“@NotEmpty”的错误,但是,它没有打印任何内容并且运行良好。
\n\n控制器:
\n\n @RequestMapping(value = "verify_smscode", method = RequestMethod.POST)\n @ResponseBody\n public ResponseDto verifySmsCode(HttpServletRequest request,\n @Valid @RequestBody VerifySmsCodeParam params, Errors errors) {\n if(errors.hasErrors()) {\n System.out.println("error");\n }\n\n boolean success = userService.verifySmsCode(params.getPhone(), params.getSmsCode());\n\n Map<String, Object> result = new HashMap<>();\n result.put("status", success);\n return new ResponseDto(result);\n }\nRun Code Online (Sandbox Code Playgroud)\n\n模型:
\n\n@Data\n@NoArgsConstructor\npublic class VerifySmsCodeParam {\n\n @NotEmpty //import org.hibernate.validator.constraints.NotEmpty;\n private String phone;\n\n @NotEmpty\n private String smsCode;\n}\nRun Code Online (Sandbox Code Playgroud)\n\nSpringMvc配置:
\n\n@Configuration\n@ComponentScan(basePackages="com.shit.voiceshare")\n@EnableWebMvc\npublic class SpringMvcConfig extends WebMvcConfigurerAdapter {\n @Override\n public Validator getValidator() {\n LocalValidatorFactoryBean localValidatorFactoryBean …Run Code Online (Sandbox Code Playgroud) 在 MVC 中,您会期望控制器接收模型作为输入并生成模型作为输出。在 Swagger 中,后者不是问题,但我对前者有麻烦。我无法理解如何让 Swagger 从传入的 GET 参数构建输入模型。
考虑:
"paths": {
"/search": {
"get": {
"consumes": [],
"produces": [
"application/json"
],
"parameters": [
// What goes here?
],
"responses": {
"200": {
"description": "Success",
"schema": {
"$ref": "#/definitions/SearchResponse"
}
},
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何使生成的控制器的方法具有如下签名:
public ResponseEntity<ResultModel> controllerGet(ModelFromParameters input);
Run Code Online (Sandbox Code Playgroud)
其中 ModelFromParameters 将有多个字段对应于不同的 GET 参数。
示例主要集中在 POST 请求或 GET 请求上,其中每个 20 多个参数都填充在方法的参数列表中,这显然是反 MVC 的。
有问题的 API 是一个复杂的无状态查询系统,有很多参数。
spring-mvc ×7
java ×5
spring ×4
spring-boot ×2
enums ×1
get ×1
json ×1
rest ×1
spring-cloud ×1
swagger ×1
validation ×1