【问题标题】:gradle spring boot webservice return json " java.lang.IllegalArgumentException " no convertgradle spring boot webservice返回json“java.lang.IllegalArgumentException”没有转换
【发布时间】:2017-06-10 11:02:59
【问题描述】:

我正在尝试创建一个返回 json 的简单 Spring Boot Web 服务,但我收到此错误“java.lang.IllegalArgumentException: No converter found for return value of type: testSomething”,即使我有 jackson json 依赖项在我的 gradle.build 中通过 spring-boot-starter-web

休息控制器。

 package controller;

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.*;
    import com.anders.cphbusiness.numbersModel.testSomething;


    @RestController
    @RequestMapping("/test")
    public class restController {

        public restController() {
        }

        @RequestMapping(method=RequestMethod.GET)
        public @ResponseBody testSomething test() {
            return new testSomething("asd", 5);
        }

    }

模型。

package numbersModel;

public class testSomething {

    private String msg;
    private int aNumber;

    public testSomething(String msg, int aNumber) {
        this.msg = msg;
        this.aNumber = aNumber;
    }

    @Override
    public String toString() {
        return msg;
    }
}

添加

compile('org.springframework.boot:spring-boot-starter-web')

在我的 gradle.build 文件中支持 jackson json。

【问题讨论】:

    标签: json web-services rest gradle spring-boot


    【解决方案1】:

    也尝试添加:

     compile("com.fasterxml.jackson.core:jackson-databind")
    

    注意:以大写字母开头的班级名称也是一个好习惯。

    public class testSomething
    

    public class TestSomething
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      • 2017-06-27
      • 1970-01-01
      • 2019-04-19
      • 2023-02-15
      • 1970-01-01
      相关资源
      最近更新 更多