Spring Boot集成测试 - TestRestTemplate如何将响应Content-Type设置为UTF-8

时间:2017-03-23 05:04:55

标签: spring-boot content-type resttemplate spring-test

我尝试集成测试Spring Boot应用程序,现在我遇到了一个问题

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class org.springframework.http.ResponseEntity] and content type [application/x-json;charset=iso-8859-1]

    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:109)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:917)
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:901)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)
    at org.springframework.boot.test.web.client.TestRestTemplate.exchange(TestRestTemplate.java:735)

如果我在下面

    @Before
    public void init() {
        List<HttpMessageConverter<?>> converters = restTemplate.getRestTemplate().getMessageConverters();
        for (HttpMessageConverter converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
                jsonConverter.setObjectMapper(new ObjectMapper());
                jsonConverter.setSupportedMediaTypes(ImmutableList
                        .of(new MediaType("application", "x-json", Charset.forName("iso-8859-1"))));
            }
        }
    }

我在MappingJackson2HttpMessageConverter中添加了application / x-json和iso8859-1支持,它可以正常工作。 这是一种解决方法,但我想知道为什么响应内容类型是application / x-json; charset = iso-8859-1?我在我的控制器中有生产配置

@RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = "application/json;charset=UTF-8")

有人知道如何配置它并让响应使用&#34; application / json; charset = UTF-8&#34;内容类型?

以下是我的测试方法

    @Autowired
    protected TestRestTemplate restTemplate;
    @Test
    public void testGetPerformanceObligationById() {
        PerformanceObligationEntity entity = restTemplate.getForObject("/performance-obligations/{id}", PerformanceObligationEntity.class, "InvalidId");
        Assert.assertNull(entity.getId());
    }

我添加了一个过滤器

    @TestConfiguration
    static class Config {
        @Bean
        public CharacterEncodingFilter characterEncodingFilter() {
            final CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
            characterEncodingFilter.setEncoding("UTF-8");
            characterEncodingFilter.setForceEncoding(true);
            return characterEncodingFilter;
        }

    }

同时将转换器修改为

    @Before
    public void init() {
        List<HttpMessageConverter<?>> converters =
                restTemplate.getRestTemplate().getMessageConverters();
        for (HttpMessageConverter converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
                jsonConverter.setObjectMapper(new ObjectMapper());
                jsonConverter.setSupportedMediaTypes(ImmutableList
                        .of(new MediaType("application", "x-json", Charset.forName("UTF-8"))));
            }
        }
    }

现在我得到了application / x-json; charset = utf-8,所以只保留如何从application / x-json配置到application / json

0 个答案:

没有答案