2017-08-01 141 views
0

錯誤日誌:配置MappingJacksonHttpMessageConverter越來越BeanCreationException

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'converter' defined in class path resource 
[...../spring/controller/PsvJackson2HttpMessageConverter.class]: 
No matching factory method found: factory bean 'psvJackson2HttpMessageConverter'; 
factory method 'converter()'. 
Check that a method with the specified name exists and that it is non-static. 

這裏是配置類:

@Configuration 
public class PsvJackson2HttpMessageConverter extends WebMvcConfigurationSupport { 

    @Override 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
     converters.add(converter()); 
     converters.add(new ByteArrayHttpMessageConverter()); 
     converters.add(new StringHttpMessageConverter()); 
     super.addDefaultHttpMessageConverters(converters); 
    } 

    @Bean 
    MappingJackson2HttpMessageConverter converter() { 
     MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); 
     converter.setObjectMapper(objectMapper); 
     return converter; 
     } 
} 

不知道這裏有什麼問題。

回答

0

這裏是你可以做的事:

  1. 創建一個配置類,其註冊/映射器。
  2. 創建自定義轉換器類。

例如,下面是配置類:

@EnableWebMvc 
@Configuration 
public class WebConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureMessageConverters(final List<HttpMessageConverter<?>> converters) { 
     converters.add(new CustomMappingJackson2HttpMessageConverter()); 
     super.configureMessageConverters(converters); 
    } 
} 

這裏是轉換器類:

public class CustomMappingConverter extends AbstractJackson2HttpMessageConverter { 


    public CustomMappingConverter(final ObjectMapper objectMapper){ 
     super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"), new MediaType("application", "jsonp")); 
    } 

    private String jsonPrefix; 


    public CustomMappingConverter() { 
     this(Jackson2ObjectMapperBuilder.json().build()); 
    } 


    public void setJsonPrefix(final String jsonPrefix) { 
     this.jsonPrefix = jsonPrefix; 
    } 


    public void setPrefixJson(final boolean prefixJson) { 
     jsonPrefix = prefixJson ? ")]}', " : null; 
    } 


    @Override 
    protected void writePrefix(final JsonGenerator generator, final Object object) throws IOException { 
     if (jsonPrefix != null) { 
      generator.writeRaw(jsonPrefix); 
     } 
     final String jsonpFunction = 
       object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null; 
       if (jsonpFunction != null) { 
        generator.writeRaw("/**/"); 
        generator.writeRaw(jsonpFunction + "("); 
       } 
    } 

    @Override 
    protected void writeSuffix(final JsonGenerator generator, final Object object) throws IOException { 
     final String jsonpFunction = 
       object instanceof MappingJacksonValue ? ((MappingJacksonValue) object).getJsonpFunction() : null; 
       if (jsonpFunction != null) { 
        generator.writeRaw(");"); 
       } 
    } 

}