Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java dropwizard中的默认属性命名策略_Java_Json_Dropwizard - Fatal编程技术网

Java dropwizard中的默认属性命名策略

Java dropwizard中的默认属性命名策略,java,json,dropwizard,Java,Json,Dropwizard,在dropwizard项目中,我有bean类,它们在资源类中使用,如下所示: @GET @Path("/{id}") public Response getUser(@PathParam("id") Long id) { return Response.ok(userDAO.get(id)).build(); } class User { private String id; @JsonProperty("first_name") private Strin

在dropwizard项目中,我有bean类,它们在资源类中使用,如下所示:

@GET
@Path("/{id}")
public Response getUser(@PathParam("id") Long id) {
    return Response.ok(userDAO.get(id)).build();
}

class User {

    private String id;

    @JsonProperty("first_name")
    private String firstName;

    @JsonProperty("last_name")
    private String lastName;
}
{
    "firstName": "John",
    "lastName": "Doe"
}

I would rather like it to be :
{
    "first_name": "John",
    "last_name": "Doe"
}
是否有一种方法可以添加到dropwizard配置或应用程序类中,让javax使用snake_case命名策略将bean实体(用户)映射到json。这将帮助我避免对类的每个成员使用@JsonProperty(“first_name”)注释

在没有上述注释的情况下,我的json如下所示:

@GET
@Path("/{id}")
public Response getUser(@PathParam("id") Long id) {
    return Response.ok(userDAO.get(id)).build();
}

class User {

    private String id;

    @JsonProperty("first_name")
    private String firstName;

    @JsonProperty("last_name")
    private String lastName;
}
{
    "firstName": "John",
    "lastName": "Doe"
}

I would rather like it to be :
{
    "first_name": "John",
    "last_name": "Doe"
}

找到另一个SO问题的答案:

将以下行添加到应用程序即可完成此任务

environment.getObjectMapperFactory()
  .setPropertyNamingStrategy(
    PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)

可以使用注释整个图元,以避免注释每个属性

否则,您可以在应用程序初始化中配置全局ObjectMapper

objectMapper.setPropertyNamingStrategy(
  PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);