RestTemplate使用“\”内容读取字符串数据(即使使用StringHttpMessageConverter)

时间:2017-03-07 01:38:03

标签: spring spring-mvc spring-rest spring-mvc-test

我正在使用Spring Framework 4.3.6.RELEASE

为了测试目的通过RestTemplate消费者,如果HTTP DELETE方法由于实体不存在而失败,我需要以原始格式或String返回错误消息。这是因为'通常'DELETE并不强制要求AcceptContent-Type。我不希望用户为AcceptXML

设置JSON会造成复杂化

我的@Test方法因以下错误而失败

java.lang.AssertionError: 
Expected: is "The entity does not exist"
     but: was "\"The entity does not exist\""

观察\"部分

在服务器端,我已声明:

@Bean
public StringHttpMessageConverter stringHttpMessageConverter(){
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setSupportedMediaTypes(Arrays.asList(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8)));
        return converter;
}

已注册:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(httpMessageConverterConfig.mappingJackson2HttpMessageConverter());
        converters.add(httpMessageConverterConfig.mappingJackson2XmlHttpMessageConverter());
        converters.add(httpMessageConverterConfig.stringHttpMessageConverter());
}

使用@ControllerAdvice,与使用@ExceptionHandler注释的方法一起使用时,会在某个位置使用以下方法创建ResponseEntity<Object>对象以建立Content-Type

if(httpServletRequest.getMethod().equals(HttpMethod.DELETE.toString())){
    headers.setContentType(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8));
}

当我在服务器端打印错误消息时,它打印时没有\"部分

用于测试目的的RestTemplate创建于:

RestTemplate restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));
restTemplate.setMessageConverters(customConverters);
restTemplate.setErrorHandler(restTemplateResponseErrorHandler);

为什么\"部分到达RestTemplate? 如何解决它。缺少什么配置需要它?

α

测试类声明

@Transactional
@WebAppConfiguration
@RunWith(Parameterized.class)
@ActiveProfiles(resolver=TestActiveProfilesResolver.class)
@ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
@TestExecutionListeners(listeners={LoggingTestExecutionListener.class}, mergeMode=MergeMode.MERGE_WITH_DEFAULTS)
public class PersonaDeleteOneRestControllerClientBadRequestTest {

该类中的一些变量:

@Autowired
private WebApplicationContext webApplicationContext;

@Autowired
private RestTemplateClientSupport restTemplateClientSupport;

private MockMvc mockMvc;

private RestTemplate restTemplate;
private static RestTemplateResponseErrorHandler restTemplateResponseErrorHandler;

一些创作:

@BeforeClass
public static void setUp_(){
    restTemplateResponseErrorHandler = new RestTemplateResponseErrorHandler();
}

@Before
public void setUp(){
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    restTemplate = restTemplateClientSupport.deleteOneBadRequest(mockMvc);
    restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
}

RestTemplate部分是:

@Component
public class RestTemplateClientSupport {

private static final Logger logger = LoggerFactory.getLogger(RestTemplateClientSupport.class.getSimpleName());

private final List<HttpMessageConverter<?>> customConverters;

@Autowired
public RestTemplateClientSupport(List<HttpMessageConverter<?>> customConverters){
    this.customConverters = customConverters;
}

private RestTemplate createRestTemplateWithListHttpMessageConverter(MockMvc mockMvc){
    RestTemplate restTemplate = new RestTemplate(new MockMvcClientHttpRequestFactory(mockMvc));

    restTemplate.getMessageConverters().removeIf(converter -> {
        if(converter.getClass().getSimpleName().equals(MappingJackson2XmlHttpMessageConverter.class.getSimpleName())){
            logger.info("Removing: {}" , converter.getClass().getSimpleName());
            return true;
        }
        else{
            return false;
        }
    });
    restTemplate.getMessageConverters().removeIf(converter -> {
        if(converter.getClass().getSimpleName().equals(MappingJackson2HttpMessageConverter.class.getSimpleName())){
            logger.info("Removing: {}" , converter.getClass().getSimpleName());
            return true;
        }
        else{
            return false;
        }
    });

    restTemplate.getMessageConverters().removeIf(converter -> {
        if(converter.getClass().getSimpleName().equals(StringHttpMessageConverter.class.getSimpleName())){
            logger.info("Removing: {}" , converter.getClass().getSimpleName());
            return true;
        }
        else{
            return false;
        }
    });

    restTemplate.getMessageConverters().addAll(customConverters);
    return restTemplate;

注意:我正在使用restTemplate.getMessageConverters().removeIf(Predicate<>..,因为我需要删除这3个默认转换器并添加我自己创建的转换器。

0 个答案:

没有答案