복붙노트

[SPRING] SpringMVC Jackson2HttpMessageConverter 사용자 정의가 작동하지 않습니다.

SPRING

SpringMVC Jackson2HttpMessageConverter 사용자 정의가 작동하지 않습니다.

SpringMVC4의 JSON 응답에 사용자 정의 JsonSerializer를 사용하려고합니다.

JsonSerializer를 추가하기 위해 WebMvcConfigurerAdapter 하위 클래스를 만들었습니다. 그러나 MappingJackson2HttpMessageConverter의 사용자 정의가 작동하지 않았습니다.

문제를 단순화하고, 나는 setJsonPrefix를 시도했다. 그러나 그것은 또한 작동하지 않았다. 응답은 바뀌지 않았습니다.

내 코드는 아래와 같습니다. 무엇이 잘못되었는지 말해주세요.

ControllerClass

@Controller
public class SampleController {

    @RequestMapping("/sample")
    @ResponseBody
    public ResponseModel action() {
        return new ResponseModel();
    }

    public static class ResponseModel {
        public String id = "001";
        public String text = "aaa";
    }
}

구성

@Configuration
@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

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

    @Bean
    protected MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setJsonPrefix("prefix");
        return converter;
    }
}

dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    xmlns:aop="http://www.springframework.org/schema/aop">


    <!-- base package -->
    <context:annotation-config />
    <context:component-scan base-package="jp.co.xxx.*" /><!-- my package. contains WebMvcConfiguration class -->

    <annotation-driven />

    <!-- aop -->
    <aop:aspectj-autoproxy />

</beans:beans>

노트.

스프링 주석 기반 구성을 사용하는 동안 MappingJacksonHttpMessageConverter를 구성하는 방법은 무엇입니까?

http://www.baeldung.com/spring-httpmessageconverter-rest

추신 JSONSerializer의 경우 아래에 있습니다.

@Configuration
@EnableWebMvc
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    protected CustomObjectMapper mapper;

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

    @Bean
    protected MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setObjectMapper(mapper);
        return converter;
    }
}

ObjectMapper

@Component
public class CustomObjectMapper extends ObjectMapper {
    private static final long serialVersionUID = -6987863269632420904L;

    public CustomObjectMapper() {
        setSerializationInclusion(Include.NON_NULL);
        enable(SerializationFeature.INDENT_OUTPUT);

        SimpleModule module = new SimpleModule();
        module.addSerializer(DateTime.class, new DateTimeSerializer());
        registerModule(module);
    }
}

각각의 경우에는 아무런 오류가 없었습니다. 그러나 사용자 정의가 작동하지 않았습니다.

해결법

  1. ==============================

    1.configureMessageConverters 메서드에서 다른 변환기를 추가하지 않으면 컨버터가 비어 있고 WebMvcConfigurationSupport가 addDefaultHttpMessageConverters를 호출하여 기본 MappingJackson2HttpMessageConverter가 포함 된 기본 변환기를 구성합니다.

    configureMessageConverters 메서드에서 다른 변환기를 추가하지 않으면 컨버터가 비어 있고 WebMvcConfigurationSupport가 addDefaultHttpMessageConverters를 호출하여 기본 MappingJackson2HttpMessageConverter가 포함 된 기본 변환기를 구성합니다.

    따라서 MappingJackson2HttpMessageConverter를 extendMessageConverters에 추가하면 작동하지 않습니다.

    두 가지 솔루션이 있습니다.

    미안 해요, 영어가 부러졌습니다.

  2. ==============================

    2.나는이 문제를 또한 가지고 있었고 다른 사이트 덕분에이 문제를 발견했다.

    나는이 문제를 또한 가지고 있었고 다른 사이트 덕분에이 문제를 발견했다.

    @EnableWebMvc는 XML 기반 구성과 동일합니다.

    BOTH가 있다면 extendMessageConverters가 효과적이지 않을 것입니다. XML 항목을 제거하자 마자 빙고 .. 사용자 지정 변환기가 작동하기 시작했습니다.

  3. from https://stackoverflow.com/questions/31247791/springmvc-jackson2httpmessageconverter-customization-doesnt-work by cc-by-sa and MIT license