简体   繁体   English

406 HttpMediaTypeNotAcceptableException:找不到“文本/日历”类型输出的可接受表示形式

[英]406 HttpMediaTypeNotAcceptableException: Could not find acceptable representation for “text/calendar” type output

I'm trying to serve an ICS file with Spring Boot. 我正在尝试通过Spring Boot提供ICS文件。 I can not manage to repond with the right Content-Type (i'm in Junit). 我无法使用正确的Content-Type进行响应(我在Junit中)。 I think a build a correct reponse but Spring override it. 我认为构建正确的响应,但Spring会覆盖它。

In my @RestController I have this 在我的@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);
}

and I test it this way : 我这样测试:

MvcResult mvcResult = restMembershipConfigMockMvc.perform(get("/api/meeting/ics/1")
        .accept(MediaType.ALL))
        .andExpect(status().isOk())
        .andReturn()
        ;

I tried several reponses found on similar question here in Stack overflow but I always receive a 406 status with this exception thrown HttpMediaTypeNotAcceptableException: Could not find acceptable representation response 我尝试了在堆栈溢出中类似问题上HttpMediaTypeNotAcceptableException: Could not find acceptable representation响应,但是我始终会收到406状态,并抛出HttpMediaTypeNotAcceptableException: Could not find acceptable representation响应

Here I removed the ".ics" from the path suffix to exclude this to cause the exception. 在这里,我从路径后缀中删除了“ .ics”,以排除它,从而导致异常。

Ok, I found the way. 好的,我找到了路。 There were too many wrong points in my question. 我的问题有太多错误点。 The reason why it didn't work was because Spring mvc doesn't provide an HttpMessageConverter that support the type "text/calendar". 它不起作用的原因是因为Spring mvc不提供支持类型“ text / calendar”的HttpMessageConverter

So I created one (i use ical4j library so i want my controller to respond that kind of object): 因此,我创建了一个(我使用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());
    }
}

I probably should improve the support method but at that point everything works and i'm happy to go away to other missions. 我可能应该改进支持方法,但是到那时一切都正常了,我很乐意参加其他任务。

Now we just have register the converter in Spring configuration : 现在我们只是在Spring配置中注册了转换器:

@Configuration
public class MediaTypeConfiguration extends WebMvcConfigurationSupport {

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters){
        converters.add(new ICSHttpMessageConverter());
        super.addDefaultHttpMessageConverters(converters);
    }
}

After that my controller looks like this : 之后,我的控制器如下所示:

@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);
}

At that point my only problem is that in my junit integration test my converter didn't look to be registred. 那时,我唯一的问题是,在我的junit集成测试中,我的转换器似乎没有被注册。 It was because my MockMvc was not given the converter at the mock init. 这是因为在模拟init时没有给我的MockMvc转换器。 I just had to add it in with the method setMessageConverters 我只需要用setMessageConverters方法添加它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpMediaTypeNotAcceptableException:找不到可接受的表示 - HttpMediaTypeNotAcceptableException: Could not find acceptable representation HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException:找不到可接受的表示形式 - HttpMediaTypeNotAcceptableException / HttpMediaTypeNotAcceptableException: Could not find acceptable representation Spring REST服务抛出“ 406不可接受”:org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示形式 - Spring REST service throw “406 Not Acceptable”: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation Spring Webflux - HttpMediaTypeNotAcceptableException:找不到可接受的表示 - Spring Webflux - HttpMediaTypeNotAcceptableException: Could not find acceptable representation “找不到可接受的表示形式” 406 - “Could not find acceptable representation” 406 HttpMediaTypeNotAcceptableException:在exceptionhandler中找不到可接受的表示形式 - HttpMediaTypeNotAcceptableException: Could not find acceptable representation in exceptionhandler 找不到可接受的表示形式-406 Not Acceptable错误 - Could not find acceptable representation - 406 Not Acceptable error HttpMediaTypeNotAcceptableException:找不到可接受的表示 - MediaType 产生不同的结果 - HttpMediaTypeNotAcceptableException: Could not find acceptable representation - MediaType produces different results Swagger 2.0.3 with Spring MVC 抛出 HttpMediaTypeNotAcceptableException:找不到可接受的表示 - Swagger 2.0.3 with Spring MVC throws HttpMediaTypeNotAcceptableException: Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示形式“错误 - org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation" error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM