java.lang.IllegalArgumentException:未找到类型为的返回值的转换器
使用此代码java.lang.IllegalArgumentException:未找到类型为的返回值的转换器,java,spring,spring-boot,jackson,Java,Spring,Spring Boot,Jackson,使用此代码 @RequestMapping(value = "/bar/foo", method = RequestMethod.GET) public ResponseEntity<foo> foo() { Foo model; ... return ResponseEntity.ok(model); } } 我的猜测是,该对象无法转换为JSON,因为Jackson丢失了。我不明白为什么,因为我以为杰克逊是用弹簧靴
@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
public ResponseEntity<foo> foo() {
Foo model;
...
return ResponseEntity.ok(model);
}
}
我的猜测是,该对象无法转换为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>
com.fasterxml.jackson.core
杰克逊核心
2.4.3
com.fasterxml.jackson.core
杰克逊数据绑定
2.4.3
我是否必须更改任何spring引导属性才能使其正常工作
谢谢使用
@ResponseBody
和getter/setter
。希望它能解决你的问题
@RequestMapping(value = "/bar/foo", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<foo> foo() {
问题是
Foo
中的一个嵌套对象没有任何getter/setter
将以下依赖项添加到pom.xml中:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0.pr3</version>
</dependency>
com.fasterxml.jackson.core
杰克逊数据绑定
2.10.0.pr3
我也遇到过这样的错误,因为我在类中的不同属性上意外地放置了两个@JsonProperty(“some_value”)相同的行我遇到了同样的问题,不幸的是,添加getter方法或添加jackson依赖项都无法解决
然后,我查看了官方的Spring指南,并遵循了这里给出的示例——其中的示例还显示了将返回的对象转换为JSON格式的过程
然后,我再次创建了自己的项目,不同的是,这次我还添加了依赖项和构建插件,它们出现在我上面提到的官方Spring指南示例的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>
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
您可以在上面提到的链接中看到相同的内容
神奇的是,至少对我来说,它起作用了。因此,如果您已经用尽了其他选择,您可能想尝试一下,就像我一样。
顺便说一句,当我在上一个项目中添加依赖项并进行Maven安装和更新项目内容时,它对我不起作用。所以,我不得不再次从头开始做我的项目。我没有太多的麻烦,因为我的是一个例子项目,但你可能也想看看 问题发生在我的案例中,因为spring framework无法获取嵌套对象的属性。getter/setter是解决问题的一种方法。将属性公开是验证这是否真的是问题的另一个快速而肮脏的解决方案。我很长时间以来都面临着同样的问题,然后才知道必须使用对象映射器将对象转换为JSON,并将其作为JSON对象传递
@RequestMapping(value=“/getTags”,method=RequestMethod.GET)
public@ResponseBody String getTags(@RequestParam String标记名)抛出
JsonGenerationException、JsonMappingException、IOException{
列表结果=新建ArrayList();
用于(标记:数据){
如果(tag.getTagName().contains(标记名)){
结果。添加(标签);
}
}
ObjectMapper ObjectMapper=新的ObjectMapper();
字符串json=objectMapper.writeValueAsString(结果);
返回json;
}
在我的例子中,我忘记添加库jackson-core.jar,只添加了jackson-annotations.jar和jackson-databind.jar。当我添加jackson-core.jar时,它修复了这个问题。添加错误消息中提到的bean中缺少的getter/setter。我有一段时间收到了相同的错误。我已经验证了getter方法可用于所有属性。仍然收到相同的错误。
要解决问题,请使用配置MVC xml(配置)
。这是Spring检测jackson的存在并设置相应转换器所必需的。@config类上的EnableWebMvc注释解决了我的问题。(Spring 5,没有web.xml,由AbstractAnnotationConfigDispatchersServletInitializer初始化)当
jackson数据绑定的范围设置为测试时,我看到了相同的错误:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
<scope>test</scope>
</dependency>
com.fasterxml.jackson.core
杰克逊数据绑定
2.9.9
测试
删除
行修复了该问题。最近遇到了相同的错误-pojo有getter/setter,所有jackson依赖项都正确地导入到pom中,但如何为jackson依赖项“提供”,这导致了该问题。从jackson dependency中删除“”修复了使用Spring Boot 2.2时出现的问题,我在谷歌搜索错误消息时遇到了类似的错误消息
没有预设内容类型为“null”的[class java.util.ArrayList]的转换器
这个问题在上面,但这里的所有答案对我来说都不适用,所以我认为添加我自己找到的答案是一个好主意:
我必须将以下依赖项添加到pom.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>
<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>
现在我得到了JSON和XML返回值,具体取决于请求Accept
头
为了使XML输出更具可读性(从标记名中删除完整的包名),您还可以将以下内容添加到实体类中:
@Table("ExampleTypes")
@XStreamAlias("ExampleType")
public class ExampleTypeEntity
{
// ...
}
希望这能帮助其他人解决同样的问题。我也面临同样的问题,但我使用的是Lombok,我的UploadFileResponse pojo是一个构建器
public ResponseEntity<UploadFileResponse>
在我的例子中,我使用的是spring boot,我遇到了一个类似的错误:
No converter for [class java.util.ArrayList] with preset Content-Type 'null'
原来我有一个控制器
@GetMapping(produces = { "application/xml", "application/json" })
遗憾的是,我没有将Accept
头添加到我的请求中您没有任何getter/setter方法。在我的例子中,我在响应实体中返回布尔值
并且:
produces = MediaType.TEXT_PLAIN_VALUE,
当我改变它的时候
@Table("ExampleTypes")
@XStreamAlias("ExampleType")
public class ExampleTypeEntity
{
// ...
}
public ResponseEntity<UploadFileResponse>
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class UploadFileResponse
No converter for [class java.util.ArrayList] with preset Content-Type 'null'
@GetMapping(produces = { "application/xml", "application/json" })
produces = MediaType.TEXT_PLAIN_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
@PostMapping(value = "/xxx-xxxx",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Boolean> yyyy(
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>