HttpMessageNotWritableException:没有使用OpenApi Spring生成器的带有预设Content-Type'null'的转换器

问题描述

我正在(渐进式)春季启动项目中使用openAPI实现petstore API。我使用openapi生成器插件生成服务器并实现一个简单的请求:

@Service
@RestController
public class PetApiController implements PetApi {

    @Override
    public ResponseEntity<Pet> getPetById(Long petId) {
        Pet p = new Pet();
        p.setId(0L);
        p.setName("fido");
        p.setPhotoUrls(new ArrayList<String>());
        p.setStatus(StatusEnum.AVAILABLE);
        p.setTags(new ArrayList<Tag>());
        Category c = new Category();
        c.setId(3L);
        c.setName("dummy category");
        p.setCategory(c);
        
        ResponseEntity<Pet> r = new ResponseEntity<Pet>(p,HttpStatus.OK);
        
        return r;
    }
}

我生成的swagger-ui为请求提供了xml和json查询,但是xml似乎不起作用:

$ curl -X GET "http://localhost:8080/pet/1" -H  "accept: application/xml"; echo ""

$ curl -X GET "http://localhost:8080/pet/1" -H  "accept: application/json"; echo ""
{"id":0,"name":"fido","category":{"id":3,"name":"dummy category"},"photoUrls":[],"tags":[],"status":"available"}

我什至收到错误消息:

2020-09-10 09:04:34.213  WARN 23958 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.petstore.server.model.Pet] with preset Content-Type 'null']

但是我无法捕获此异常并打印整个上下文,它仅在return r

之后发生

我的确切错误消息已经存在一个问题: Springboot HttpMessageNotWritableException: No converter for [...] with preset Content-Type 'null']但是我仍然认为上下文不同,因为我生成了POJO,所以我无法控制它们,并且因为插件应该适当地生成所有内容,所以我一开始就不会遇到这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)