2

My app use spring boot with lombok, when I use @Data annation will cause

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

When I expicity write getter and setter, it works good.
Please help me, thanks; The code:

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/{id}")
    public User query(@PathVariable long id) {
        if (id == 1L) {
            return new User(1l);
        } else {
            return new User(2L);
        }
    }

}

@Data
public class User {
    private long userId;
    private String userName;
    private String password;
    private String mobile;
    private String address;

    public User() {
    }
    public User(long userId){
        this(userId, "zhengfc", "pwd", "13322222222", "shanghai-zhengjiang");
    }
    public User(long userId, String userName, String password, String mobile, String address) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
        this.mobile = mobile;
        this.address = address;
    }

}
user3172755
  • 137
  • 1
  • 10
  • 1
    It could be related to: http://stackoverflow.com/a/37842512/475116 So are you sure `lombok` is well configurated? It seems is not generating getters and setters. – Pau Oct 21 '16 at 07:03
  • For me personal experience, lombok Getters/Setters annotation works perfectly, but the Data sometimes get errors problems, i would suggest you use the Getter/Setter at property level – cralfaro Oct 21 '16 at 12:45
  • Thanks for you answer, I solve, It cause by the Intellij idea `lombok` not well configurated – user3172755 Nov 22 '16 at 03:37

2 Answers2

1

I had the same issue and this is what I did to corrected it If you have use maven to download the lombok dependency as I did using the below dependency

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
</dependency>

go to the maven repository in your local for example for me : @ C:\Users.m2\repository\org\projectlombok\lombok\1.16.20\lombok-1.16.20.jar

copy the lombok jar(as specified above) to the root folder of your STS or eclipse for me(I use STS) I copied to D:\Public\software_executables\STS\spring-tool-suite-3.9.5.RELEASE-e4.8.0-win32-x86_64\sts-bundle\sts-3.9.5.RELEASE

Then open command prompt from that location [where you have copied the lombok jar now] and run the following command java -jar lombok-1.16.20.jar (use the version of lombok you are using)

You will get a Pop up asking for the location of your eclipse or STS .exe give the right path and click on [Install/Update] Button

NOTE: DURING ALL THE ABOVE OPERATION STS/Eclipse should have been close

Now open STS/Eclipse and clean your projects And ALL YOUR Issue's with LOMBOK SHOULD BE CORRECTED

HOPE THIS HELPS

JACOB
  • 11
  • 2
0

For me personal experience,I tried it like yours.

this is the model

``` package info.xiaomo.website.controller;

import lombok.Data;

/**
 * 把今天最好的表现当作明天最新的起点..~
 * いま 最高の表現 として 明日最新の始発..~
 * Today the best performance  as tomorrow newest starter!
 * Created by IntelliJ IDEA.
 *
 * @author: xiaomo
 * @github: https://github.com/qq83387856
 * @email: hupengbest@163.com
 * @QQ_NO: 83387856
 * @Date: 2016/11/8 10:29
 * @Description: 用户实体类
 * @Copyright(©) 2015 by xiaomo.
 **/

@Data
public class Test {
    private long userId;
    private String userName;
    private String password;
    private String mobile;
    private String address;

    public Test() {
    }
    public Test(long userId){
        this(userId, "zhengfc", "pwd", "13322222222", "shanghai-zhengjiang");
    }
    public Test(long userId, String userName, String password, String mobile, String address) {
        this.userId = userId;
        this.userName = userName;
        this.password = password;
        this.mobile = mobile;
        this.address = address;
    }

}

```

this is the controller

```

package info.xiaomo.website.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 把今天最好的表现当作明天最新的起点..~
 * いま 最高の表現 として 明日最新の始発..~
 * Today the best performance  as tomorrow newest starter!
 * Created by IntelliJ IDEA.
 *
 * @author: xiaomo
 * @github: https://github.com/qq83387856
 * @email: hupengbest@163.com
 * @QQ_NO: 83387856
 * @Date: 2016/11/8 10:29
 * @Description: 用户实体类
 * @Copyright(©) 2015 by xiaomo.
 **/

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


    @GetMapping("/{id}")
    public Test query(@PathVariable long id) {
        if (id == 1L) {
            return new Test(1l);
        } else {
            return new Test(2L);
        }

    }
}

```

and I run the server,and http://localhost:8080/test/1

enter image description here

so,I think that your code is correct。If you still get the Error,I sugguest that you need check you project environment.

Evan Hu
  • 60
  • 8