gpt4 book ai didi

java - 如何在 Spring MVC 的 JavaBean 中将下划线样式查询字符串转换为 Camel 样式属性?

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:26 26 4
gpt4 key购买 nike

例如,这是一个 Get 请求:

get: /search?product_category=1&user_name=obama

我想定义一个 SearchRequest 来接受查询字符串,这样我就可以使用 JSR 303 bean 验证注解来验证参数,如下所示:

public class SearchRequest {
@NotEmpty(message="product category is empty")
private int productCategory;
@NotEmpty(message="user name is empty")
private int userName;
}

那么jackson中有没有类似@JsonProperty的东西把下划线风格转成 Camel 风格呢?

最佳答案

你只有两个选择;

首先。让您的 SearchRequest pojo 带有用于验证的注释值,但让 Controller POST 方法接收 pojo 作为 JSON/XML 格式的请求正文。

public class SearchRequest {
@NotEmpty(message="product category is empty")
private int productCategory;
@NotEmpty(message="user name is empty")
private int userName;
}

public String search(@RequestBody @Valid SearchRequest search) {
...
}

其次。在 Controller 方法签名本身中进行验证消除了 pojo 中的验证,但如果需要,您仍然可以使用 pojo。

public class SearchRequest {

private int productCategory;

private int userName;
}

public String search(@RequestParam("product_category") @NotEmpty(message="product category is empty") String productCategory, @RequestParam("user_name") @NotEmpty(message="user name is empty") String username) {
... // Code to set the productCategory and username to SearchRequest pojo.
}

关于java - 如何在 Spring MVC 的 JavaBean 中将下划线样式查询字符串转换为 Camel 样式属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com