Programing

java.lang.IllegalArgumentException : 유형의 반환 값에 대한 변환기가 없습니다.

lottogame 2020. 10. 28. 07:38
반응형

java.lang.IllegalArgumentException : 유형의 반환 값에 대한 변환기가 없습니다.


이 코드로

@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

내 생각 엔 Jackson이 없기 때문에 객체를 JSON으로 변환 할 수 없다는 것입니다. 나는 잭슨이 스프링 부츠와 함께 내장되었다고 생각했기 때문에 이유를 이해하지 못합니다.

그런 다음 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>

이 작업을 수행하려면 스프링 부트 속성을 변경해야합니까?

감사합니다


문제는 Foo의 중첩 된 개체 중 하나에 getter/setter


pom.xml에 아래 종속성을 추가하십시오.

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

오류 메시지에 언급 된 빈 내부에 누락 된 getter / setter를 추가하십시오.


사용 @ResponseBody하고 getter/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>

내 경우에는 스프링 프레임 워크가 중첩 된 객체의 속성을 가져올 수 없기 때문에 문제가 발생했습니다. 게터 / 세터는 해결 방법 중 하나입니다. 속성을 공개하는 것은 이것이 실제로 문제인지 확인하는 또 다른 빠르고 더러운 솔루션입니다.


나는 똑같은 문제가 있었고 불행히도 getter 메서드를 추가하거나 jackson 종속성을 추가하여 해결할 수 없었습니다.

그런 다음 공식 Spring Guide를보고 여기에 제공된 예제 ( 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이 프로젝트 항목을 설치 및 업데이트 할 때 작동하지 않았습니다. 그래서 처음부터 다시 프로젝트를 만들어야했습니다. 저의 프로젝트는 예제 프로젝트이므로 신경 쓰지 않았습니다.하지만 당신도 찾아보고 싶을 것입니다!


@EnableWebMvc annotation on config class resolved my problem. (Spring 5, no web.xml, initialized by AbstractAnnotationConfigDispatcherServletInitializer)


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

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

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


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


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

 <mvc:annotation-driven/>

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


I saw the same error when the scope of the jackson-databind dependency had been set to test:

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

Removing the <scope> line fixed the issue.

참고URL : https://stackoverflow.com/questions/37841373/java-lang-illegalargumentexception-no-converter-found-for-return-value-of-type

반응형