我正在使用Spring Framework 4.3.6.RELEASE
为了测试目的通过RestTemplate
消费者,如果HTTP DELETE
方法由于实体不存在而失败,我需要以原始格式或String
返回错误消息。这是因为'通常'DELETE
并不强制要求Accept
和Content-Type
。我不希望用户为Accept
或XML
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个默认转换器并添加我自己创建的转换器。