resteasy框架之PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy

resteasy框架之PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy

resteasy是一个实现 JAX-RS specification的框架。 


其中有josn序列化是用的jackson。序列化中对象的属性与输出的json格式key名字的策略由PropertyNamingStrategy决定。

比如:

com.fasterxml.jackson.databind.PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy


A PropertyNamingStrategy that translates typical camel case Java property names to lower case JSON element names, separated by underscores. This implementation is somewhat lenient, in that it provides some additional translations beyond strictly translating from camel case only. In particular, the following translations are applied by this PropertyNamingStrategy.


Every upper case letter in the Java property name is translated into two characters, an underscore and the lower case equivalent of the target character, with three exceptions.
For contiguous sequences of upper case letters, characters after the first character are replaced only by their lower case equivalent, and are not preceded by an underscore.
This provides for reasonable translations of upper case acronyms, e.g., "theWWW" is translated to "the_www".
An upper case character in the first position of the Java property name is not preceded by an underscore character, and is translated only to its lower case equivalent.
For example, "Results" is translated to "results", and not to "_results".
An upper case character in the Java property name that is already preceded by an underscore character is translated only to its lower case equivalent, and is not preceded by an additional underscore.
For example, "user_Name" is translated to "user_name", and not to "user__name" (with two underscore characters).
If the Java property name starts with an underscore, then that underscore is not included in the translated name, unless the Java property name is just one character in length, i.e., it is the underscore character. This applies only to the first character of the Java property name.
These rules result in the following additional example translations from Java property names to JSON element names.
"userName" is translated to "user_name"
"UserName" is translated to "user_name"
"USER_NAME" is translated to "user_name"
"user_name" is translated to "user_name" (unchanged)
"user" is translated to "user" (unchanged)
"User" is translated to "user"
"USER" is translated to "user"
"_user" is translated to "user"
"_User" is translated to "user"
"__user" is translated to "_user" (the first of two underscores was removed)
"user__name" is translated to "user__name" (unchanged, with two underscores)


resteasy框架中默认的PropertyNamingStrategy是属性名和join的key名字是一样的。如何使用PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy,使得join的key的名字用下划线分割。

一种方式是用注解:

 @JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)


另一种方式利用reseaty spi依赖注入机制,注入我们自己定义的规则:

比如:

package com.doctor.demo.common.dto;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

/**
 * @author sdcuike
 *
 *         Create At 2016年3月20日 下午12:10:08
 */
@Provider
@Consumes({ MediaType.APPLICATION_JSON, "text/json" })
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class JacksonConfig extends ResteasyJackson2Provider {
    private final ObjectMapper objectMapper;

    public JacksonConfig() {
        this.objectMapper = new ObjectMapper();
        this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        setMapper(objectMapper);
    }

}


上面扩展了resteasy自带的ResteasyJackson2Provider,根据resteasy spi的规则,我们必须在

/META-INF/services/javax.ws.rs.ext.Providers  文件中写入:

com.doctor.demo.common.dto.JacksonConfig。


效果:

resteasy框架之PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy_第1张图片


你可能感兴趣的:(resteasy)