Spring RestController-找不到类型为java.lang.Integer的返回值的转换器 [英] Spring RestController - No converter found for return value of type java.lang.Integer

查看:545
本文介绍了Spring RestController-找不到类型为java.lang.Integer的返回值的转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试用spring创建我的第一个休息服务,我不希望他返回 XML结果(基于JAXB).

I am currently trying to create my first rest service with spring, I wan't him to return XML result (based on JAXB).

在一个非常基本的休息控制器上:

on a very basic rest controller:

import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public @ResponseBody
    Integer getResume() {
        return 5;
    }
}

带有spring-servlet.xml的空白:

with a spring-servlet.xml pretty empty:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="fr.urouen"
    />
    <mvc:annotation-driven />
</beans>

在JDK8下还有一个Tomcat服务器,因此我可以确保所有模块默认情况下都在JVM上加载...我抛出了以下异常:

And a Tomcat server under JDK8 so I can be sure that all modules are load by default on the JVM... I got the following exception throwed:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.lang.Integer

我不愿意使用Jackson,因为我知道JAXB可以解决这个问题. 但是在弄乱了我的代码的一部分之后,我仍然找不到她为什么被扔的答案.

I don't wan't to use Jackson because I know that JAXB can handle this. But after juggle with some part of my code, I still can't found the answer of why she is throwed.

我试图用getters创建一个虚拟类,如答案所述

I tried to create a dummy class with getters as answers said

public class Dummy {
    private int value;


    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

@RestController
public class CVIController {
    @RequestMapping(value = "/resume")
    public ResponseEntity<Dummy> getResume() {
        Dummy dummy = new Dummy();
        dummy.setValue(5);
        return new ResponseEntity<Dummy>(dummy, HttpStatus.OK);
    }
}

但仍然得到完全相同的结果:

But still got the exact same result :

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class fr.urouen.model.Dummy

推荐答案

最简单的解决方案是返回String并删除@ResponseBody批注.根据定义,作为rest控制器,将返回一个字符串,这是此特定示例的方法.

The simplest solution is to return a String and remove the @ResponseBody annotation. As a rest controller, by definition, returns a string, that's the way to go for this specific example.

如果您正在寻求更全面的解决方案,则Spring默认情况下使用Jackson进行从对象到JSON的转换. @ResponseBody会自动执行此操作.

If you are after a more comprehensive solution, Spring by default uses Jackson to do the conversion from object to JSON. The @ResponseBody does that automatically.

我非常确定Integer不会起作用,因为它不是带有getter和setter以及可以转换为JSON的POJO.即{5}不是有效的JSON对象.

Im pretty sure that Integer won't work because it's not a POJO with getters and setters and something that can be converted to JSON. ie { 5 } is not a valid JSON object.

如果您有这样的课程:

public class returnObject() {
  private int value;
  ... getters/setters
}

然后执行此操作:

@RestController
@ResponseBody
public ReturnObject getValue() {
   ReturnObject ret = new ReturnObject();
   ret.setValue(5);
   return ret;
}

您将得到此信息:

{ "value" : 5 }

您还需要将jackson添加到pom:

You will also need to add jackson to your pom:

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

这篇关于Spring RestController-找不到类型为java.lang.Integer的返回值的转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆