我開發了一個使用春季啓動和春季啓動啓動程序仇恨的休息服務。而且我正面臨着定製ObjectMapper的問題。代碼低於對於相同的:自定義的ObjectMapper不與春季開機仇敵合作
Application.java
@Configuration
@Import(BillServiceConfig.class)
@EnableAutoConfiguration
@EnableEurekaClient
@ComponentScan({"com.bill"})
@EnableWebMvc
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application extends WebMvcConfigurerAdapter{
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.indentOutput(true).dateFormat(new SimpleDateFormat("MM-yyyy-dd"));
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
builder.configure(objectMapper);
return builder;
}
依賴關係:
dependencies {
compile "org.springframework.boot:spring-boot-starter-hateoas"
compile "org.springframework.boot:spring-boot-starter-ws"
compile "org.springframework.boot:spring-boot-starter-actuator"
Bill.java:
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName("bills")
public class Bill{
BillController.java:
public ResponseEntity<Resources<Resource<Bill>>> getBills(){
我得到的輸出是:
{
_embedded: {
billList:
但我需要「法案」到位「billList」的。這是因爲ObjectMapper沒有得到定製。我是否缺少任何配置,請幫助我解決這個問題。提前致謝。
'Bill'上的@JsonRootName怎麼可能對屬性名'billList'產生影響? – zeroflagL 2015-04-02 18:48:34
我有同樣的問題,你有沒有解決這個問題? – 2017-01-25 09:26:08
稍微深入一下,通過Spring源代碼和一些Google搜索,我發現這個問題描述了這個問題https://github.com/spring-projects/spring-hateoas/issues/333 – 2017-01-25 10:07:40