类型的返回值没有找到转换器

用这个密码

@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
public ResponseEntity<foo> foo() {


Foo model;
...
return ResponseEntity.ok(model);
}
}

我得到了下面的例外

java.lang.IllegalArgumentException: No converter found for return value of type

我的猜测是,对象不能转换为 JSON,因为 Jackson 不见了。我不明白为什么,因为我以为杰克逊是内置弹簧靴。

然后我尝试将 Jackson 添加到 pom.xml 中,但是仍然有同样的错误

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.3</version>
</dependency>

我必须改变任何弹簧启动属性,使这个工作?

174335 次浏览

使用 @ResponseBodygetter/setter,希望能解决你的问题。

@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<foo> foo() {

更新你的 mvc-dispatcher-servlet.xml:

<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>

问题是 Foo中的一个嵌套对象没有任何 getter/setter

将下面的依赖项添加到 pom.xml:

 <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0.pr3</version>
</dependency>

当我偶然地将两个@JsonProperty (“ some _ value”)相同的行放在类的不同属性上时,我也遇到了这样的错误

我遇到了同样的问题,不幸的是,它不能通过添加 getter 方法或者增加 jackson 依赖关系来解决。

然后我查看了“官方春季指南”,并按照这里给出的例子—— https://spring.io/guides/gs/actuator-service/——其中的例子还显示了将返回的对象转换为 JSON 格式。

然后我再次制作了自己的项目,不同的是,这次我还添加了依赖项和构建插件,这些插件出现在上面提到的官方 Spring Guide 示例的 Pom.xml文件中。

XML 文件的修改依赖项和构建部分如下所示!

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

您可以在上面提到的链接中看到相同的内容。

神奇的是,至少对我来说,它起作用了。所以,如果你已经用尽了你的其他选择,你可能想试试这个,就像我的情况一样。

顺便说一句,当我在以前的项目中添加依赖关系并进行 Maven 安装和更新项目内容时,它对我不起作用。所以,我不得不再次从头开始做我的项目。我没有太多关于它的烦恼,因为我的是一个示例项目,但你可能想要寻找太多!

这个问题发生在我的例子中,因为 Spring 框架无法获取嵌套对象的属性。Getters/Setters 是解决问题的一种方法。如果这确实是问题所在,那么将属性公开是另一个快速而肮脏的验证解决方案。

我面临同样的问题很长时间,然后才知道必须使用对象映射器将对象转换为 JSON 并将其作为 JSON Object传递

@RequestMapping(value = "/getTags", method = RequestMethod.GET)
public @ResponseBody String getTags(@RequestParam String tagName) throws
JsonGenerationException, JsonMappingException, IOException {
List<Tag> result = new ArrayList<Tag>();
for (Tag tag : data) {
if (tag.getTagName().contains(tagName)) {
result.add(tag);
}
}
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(result);
return json;
}

在我的例子中,我忘记添加库 jackson-core.jar,我只添加了 jackson-annotations.jar 和 jackson-database ind.jar。当我加入 Jackson-core.jar 时,问题就解决了。

在错误消息中提到的 bean 中添加缺少的 getter/setter。

有一段时间我得到了同样的错误。我验证了 getter 方法对所有属性都是可用的。仍然得到相同的错误。 To resolve an issue Configure MVC xml(configuration) with

 <mvc:annotation-driven/>

这对于 Spring 检测到 Jackson 的存在并设置相应的转换器是必需的。

配置类上的@EnableWebMvc 注释解决了我的问题(Spring5,没有 web.xml,由 AbstractAnnotationConfigDispatcherServletInitializer 初始化)

jackson-databind依赖项的范围设置为 test时,我看到了同样的错误:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
<scope>test</scope>
</dependency>

删除 <scope>线修复了这个问题。

最近面临同样的错误-pojo 有 getters/setter,所有的 jackson 依赖项都正确地导入到 pom 中,但是“ < scope >”是如何为 jackson 依赖项“提供”的,这就导致了问题。从 Jackson 依赖中移除“ < 范围 >”修复了这个问题

在使用 Spring Boot 2.2时,我遇到了一个类似的错误消息,而在谷歌上搜索我的错误消息时也遇到了类似的错误消息

没有预设 Content-Type‘ null’的[ class java.util. ArrayList ]转换器

这个问题是最重要的,但是所有的答案对我来说都不起作用,所以我觉得加上我自己找到的答案是个好主意:

我必须向 pom.xml添加以下依赖项:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>


<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.11.1</version>
</dependency>

在此之后,我需要向 WebApplication 类添加以下内容:

@SpringBootApplication
public class WebApplication
{
// ...


@Bean
public HttpMessageConverter<Object> createXmlHttpMessageConverter()
{
final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();
final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
xstreamMarshaller.setAutodetectAnnotations(true);
xmlConverter.setMarshaller(xstreamMarshaller);
xmlConverter.setUnmarshaller(xstreamMarshaller);
return xmlConverter;
}


}

最后但并非最不重要的在我的 @Controller我使用:

@GetMapping(produces = {MediaType.APPLICATION_XML_VALUE, MediaType. APPLICATION_JSON_VALUE})
@ResponseBody
public List<MeterTypeEntity> listXmlJson(final Model model)
{
return this.service.list();
}

So now I got JSON and XML return values depending on the requests Accept header.

为了使 XML 输出更具可读性(从标记名中删除完整的包名称) ,还可以将以下 @XStreamAlias添加到实体类中:

@Table("ExampleTypes")
@XStreamAlias("ExampleType")
public class ExampleTypeEntity
{
// ...
}

希望这能帮助其他有同样问题的人。

我也面临同样的问题,但是我使用的是龙目岛,而我的 UploadfileResponse pojo 是一个构建器。

public ResponseEntity<UploadFileResponse>

为了解决这个问题,我添加了@Getter 注释:

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class UploadFileResponse

在我的例子中,我使用的是 Spring boot,我遇到了类似的错误:

No converter for [class java.util.ArrayList] with preset Content-Type 'null'


原来我有一个控制器

@GetMapping(produces = { "application/xml", "application/json" })


可惜我没有在请求中添加 Accept头文件

you didn't have any getter/setter methods.

在我的例子中,我在响应实体中返回布尔值 并且拥有:

produces = MediaType.TEXT_PLAIN_VALUE,

When i changed it to below

produces = MediaType.APPLICATION_JSON_VALUE

成功了!

我所拥有的一切的例子。

@PostMapping(value = "/xxx-xxxx",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> yyyy(

@ Marc 的回答也是有效的。但是具体的答案是 Getter方法是必需的。你甚至不需要 Setter

在 pom.xml 中添加以下依赖项:

    <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>

面临与类 Foo 的 MediaType 无法绑定返回类型相同的问题。在添加了依赖项之后,它工作了。

这种情况也可能发生在 Jackson 版本太低的情况下; 例如,Spring Boot 2.4默认的 Jackson 版本在使用 Java 记录时太低; 您至少需要2.5才能正确地序列化它们。

您缺少一个注释 @ResponseBody

我在 Spring 5项目(不是 Spring Boot)中也遇到了同样的错误,在一个返回 ResponseEntity<List<MyPojo>>的方法上运行了一个 SpringMVC JUnit 测试用例

错误: No converter found for return value of type: class java.util.ArrayList

我以为我的诗歌里有所有正确的杰克逊艺术品,但后来意识到我有遗产版本。Maven groupId 在 Jackson 罐子上从 org.codehaus.jackson更改为 com.fasterxml.jackson.core。切换到新的罐子后,错误消失了。

更新的 maven pom.xml:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>