要将具有camel case属性的java对象序列化为带有下划线的json,我们使用PropertyNamingStrategyas SNAKE_CASE.
那么,是否还有相反的事情要做.也就是说,使用下划线将json反序列化为带有camel case的Java对象.
例如,JSON:
{
"user_id": "213sdadl"
"channel_id": "asd213l"
}
Run Code Online (Sandbox Code Playgroud)
应该反序列化到这个Java对象:
public class User {
String userId; // should have value "213sdadl"
String channelId; // should have value "asd213l"
}
Run Code Online (Sandbox Code Playgroud)
我知道这样做的一种方法是通过@JsonProperty在现场级别工作的注释.我有兴趣知道任何全球设置.
@GetMapping("item")
public @ResponseBody String get(@ModelAttribute Item item)
Run Code Online (Sandbox Code Playgroud)
Item 有属性
name
itemType
当我访问/item?name=foo&item_type=bar了item被只填充name 未用itemType.
我尝试了很多东西来获取itemType映射的属性item_type.
Item的itemType属性.这里描述.Item类级别添加了PropertyNamingStrategy .这里描述.我怎么解决这个问题?
顺便说一句.对于传入的不是传出的JSON序列化我只有这个问题Item.
更新04/24/17:
下面是一个最小的示例,用于演示问题:访问时/item您会看到"传出"的JSON序列化有效,但访问时/item/search?name=foo&item_type=bar它不适用于'传入'JSON反序列化.
项目
package sample;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
@JsonNaming(SnakeCaseStrategy.class)
public class Item implements Serializable {
private String name; …Run Code Online (Sandbox Code Playgroud) 例如,这是一个 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;
}
Run Code Online (Sandbox Code Playgroud)
那么,是否有类似@JsonPropertyjackson 的东西将下划线样式转换为骆驼样式?