java.lang.IllegalArgumentException:找不到类型返回值的转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37841373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:44:02  来源:igfitidea点击:

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

javaspringspring-bootHymanson

提问by Marc

With this code

有了这个代码

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

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

I get the following exception

我收到以下异常

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

My guess is that the object cannot be converted to JSON because Hymanson is missing. I don't understand why because I thought that Hymanson was built in with spring boot.

我的猜测是该对象无法转换为 JSON,因为缺少 Hymanson。我不明白为什么,因为我认为Hyman逊是内置弹簧靴的。

Then I have tried to add Hymanson to the pom.xml but I still have the same error

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

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

Do I have to change any spring boot properties to make this work?

我是否必须更改任何 Spring Boot 属性才能使其正常工作?

Thank you

谢谢

采纳答案by Marc

The problem was that one of the nested objects in Foo didn't have any getter/setter

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

回答by SkyWalker

Use @ResponseBodyand getter/setter. Hope it will solve your issue.

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

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

and update your mvc-dispatcher-servlet.xml:

并更新您的mvc-dispatcher-servlet.xml

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

回答by Ash_P

Add the below dependency to your pom.xml:

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

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

回答by Vit Ias

I also experienced such error when by accident put two @JsonProperty("some_value") identical lines on different properties inside the class

我也遇到过这样的错误,因为不小心在类内的不同属性上放置了两个 @JsonProperty("some_value") 相同的行

回答by thisisashwani

I had the very same problem, and unfortunately it could not be solved by adding getter methods, or adding Hymanson dependencies.

我遇到了同样的问题,不幸的是无法通过添加 getter 方法或添加 Hymanson 依赖项来解决。

I then looked at Official Spring Guide, and followed their example as given here - https://spring.io/guides/gs/actuator-service/- where the example also shows the conversion of returned object to JSON format.

然后我查看了官方 Spring 指南,并按照此处给出的示例进行操作 - https://spring.io/guides/gs/actuator-service/- 该示例还显示了返回对象到 JSON 格式的转换。

I then again made my own project, with the difference that this time I also added the dependencies and build plugins that's present in the pom.xmlfile of the Official Spring Guide example I mentioned above.

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

The modified dependencies and build part of XML file looks like this!

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>

You can see the same in the mentioned link above.

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

And magically, atleast for me, it works. So, if you have already exhausted your other options, you might want to try this out, as was the case with me.

神奇的是,至少对我来说,它有效。因此,如果您已经用尽了其他选择,您可能想尝试一下,就像我的情况一样。

Just a side note, it didn't work for me when I added the dependencies in my previous project and did Maven install and update project stuff. So, I had to again make my project from scratch. I didn't bother much about it as mine is an example project, but you might want to look for that too!

只是附带说明,当我在以前的项目中添加依赖项并执行 Maven 安装和更新项目内容时,它对我不起作用。所以,我不得不再次从头开始制作我的项目。我并没有太在意,因为我是一个示例项目,但您可能也想寻找它!

回答by vish213

The issue occurred in my case because spring framework couldn't fetch the properties of nested objects. Getters/Setters is one way of solving. Making the properties public is another quick and dirty solution to validate if this is indeed the problem.

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

回答by Kiran Kawade

I was facing same issue for long time then comes to know have to convert object into JSON using Object Mapper and pass it as JSON Object

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

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

回答by Lisa

In my case, I forgot to add library Hymanson-core.jar, I only added Hymanson-annotations.jar and Hymanson-databind.jar. When I added Hymanson-core.jar, it fixed the problem.

就我而言,我忘记添加库 Hymanson-core.jar,我只添加了 Hymanson-annotations.jar 和 Hymanson-databind.jar。当我添加 Hymanson-core.jar 时,它解决了这个问题。

回答by Soumyajit Swain

Add the getter/setter missing inside the bean mentioned in the error message.

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

回答by Nilay

I was getting the same error for a while.I had verify getter methods were available for all properties.Still was getting the same error. To resolve an issue Configure MVC xml(configuration) with

有一段时间我遇到了同样的错误。我已经验证了所有属性都可以使用 getter 方法。仍然遇到相同的错误。解决问题 Configure MVC xml(configuration) with

 <mvc:annotation-driven/>

.This is required for Spring to detect the presence of Hymanson and setup the corresponding converters.

. 这是 Spring 检测 Hymanson 的存在并设置相应转换器所必需的。