Spring HttpMessageNotWritableException:对于具有预设内容类型的[…]没有转换器';空';]使用OpenApi弹簧生成器
我正在(gradle)spring boot项目中使用openAPI实现petstore API。我使用openapi生成器插件生成了一个服务器,并实现了一个简单的请求:Spring HttpMessageNotWritableException:对于具有预设内容类型的[…]没有转换器';空';]使用OpenApi弹簧生成器,spring,spring-boot,openapi-generator,Spring,Spring Boot,Openapi Generator,我正在(gradle)spring boot项目中使用openAPI实现petstore API。我使用openapi生成器插件生成了一个服务器,并实现了一个简单的请求: @Service @RestController public class PetApiController implements PetApi { @Override public ResponseEntity<Pet> getPetById(Long petId) { Pet
@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;
}
}
我甚至收到一条错误消息:
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']
但是,我无法捕获此异常并打印整个上下文,它只发生在返回r
我的确切错误消息中已经有一个问题:
但是我仍然认为我的上下文是不同的,因为我生成了POJO,所以我无法控制它们,而且我不应该首先遇到这个问题,因为插件应该适当地生成所有内容。spring生成器默认区分xml,请参阅文档:下
对我来说,问题在于:
响应:
"200":
描述:默认响应
内容:
“*/*”#您需要在pom文件中使用以下依赖项添加对xml格式的支持:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.1.1</version>
</dependency>
com.fasterxml.jackson.dataformat
jackson数据格式xml
2.1.1
注:此版本为今天的最新版本
Option | Description | Default
--------+---------------------------------------------------+---------
withXml | whether to include support for application/xml | false
| content type and include XML annotations in the |
| model (works with libraries that provide support |
| for JSON and XML) |
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.1.1</version>
</dependency>