![](/img/trans.png)
[英]HttpMediaTypeNotAcceptableException: Could not find acceptable representation
[英]406 HttpMediaTypeNotAcceptableException: Could not find acceptable representation for “text/calendar” type output
我正在尝试通过Spring Boot提供ICS文件。 我无法使用正确的Content-Type进行响应(我在Junit中)。 我认为构建正确的响应,但Spring会覆盖它。
在我的@RestController中,我有这个
@GetMapping(value="/meeting/ics/{id}",produces = "text/calendar;charset=UTF-8")
public ResponseEntity<byte[]> getMeetingRoomCalendar(@PathVariable("id") Long id) throws ParseException, URISyntaxException, IOException {
//...
byte[]out= calendar.toString().getBytes();
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(new MediaType("text", "calendar", Charset.forName("UTF-8")));
responseHeaders.setContentLength(out.length);
return new ResponseEntity<>(out,responseHeaders,HttpStatus.OK);
}
我这样测试:
MvcResult mvcResult = restMembershipConfigMockMvc.perform(get("/api/meeting/ics/1")
.accept(MediaType.ALL))
.andExpect(status().isOk())
.andReturn()
;
我尝试了在堆栈溢出中类似问题上HttpMediaTypeNotAcceptableException: Could not find acceptable representation
响应,但是我始终会收到406状态,并抛出HttpMediaTypeNotAcceptableException: Could not find acceptable representation
响应
在这里,我从路径后缀中删除了“ .ics”,以排除它,从而导致异常。
好的,我找到了路。 我的问题有太多错误点。 它不起作用的原因是因为Spring mvc不提供支持类型“ text / calendar”的HttpMessageConverter
。
因此,我创建了一个(我使用ical4j库,因此我希望我的控制器响应这种对象):
public class ICSHttpMessageConverter<T> extends AbstractHttpMessageConverter<T>
{
public ICSHttpMessageConverter() {
super(new MediaType("text","calendar"));
}
@Override
protected boolean supports(Class<?> aClass) {
return true;
}
@Override
protected T readInternal(Class<? extends T> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException {
CalendarBuilder calendarBuilder = new CalendarBuilder();
Calendar calendar = null;
try {
calendar = calendarBuilder.build(httpInputMessage.getBody());
} catch (ParserException e) {
e.printStackTrace();
}
return (T)calendar;
}
@Override
protected void writeInternal(T t, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException {
httpOutputMessage.getBody().write(t.toString().getBytes());
}
}
我可能应该改进支持方法,但是到那时一切都正常了,我很乐意参加其他任务。
现在我们只是在Spring配置中注册了转换器:
@Configuration
public class MediaTypeConfiguration extends WebMvcConfigurationSupport {
@Override
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters){
converters.add(new ICSHttpMessageConverter());
super.addDefaultHttpMessageConverters(converters);
}
}
之后,我的控制器如下所示:
@GetMapping(value="/meeting/ics/{id}.ics")
public ResponseEntity<Calendar> getMeetingRoomCalendar(@PathVariable("id") Long id) throws ParseException, URISyntaxException, IOException {
//...
calendar = ICSMeetingRoomAgendaHandler.loadBookings(calendar,bookingDTOS,meetingRoomDTO);
return ResponseEntity.ok().body(calendar);
}
那时,我唯一的问题是,在我的junit集成测试中,我的转换器似乎没有被注册。 这是因为在模拟init时没有给我的MockMvc转换器。 我只需要用setMessageConverters
方法添加它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.