1

I'm attempting to get a basic spring application up and running from a tutorial with mysql but I'm running into issues with the GetMapping and PostMapping annotations, here is what I have so far:

I've manually added a user into my table with id=0, name="test" via mysql workbench and verified that the data is in fact there. I was forced to do it via mysql workbench because attempting to post with curl results in no change

Attempting to call localhost/api/user/1 when there is no user with that ID gives me a 404, which is what is expected while calling it with localhost/api/user/0 gives me an http ok code 200, I'm just not actually receiving a populated JSON object.

Debugging the application on the getUser (using url localhost/api/user/0) shows a user in memory with id=0, name="test" however once the return ResponseBody.ok()... is completed the response I get via browser AND curl is still {}, an empty JSON object

User.Java

@Data
@Builder
@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String name;
}

UserRepository.java

@Repository
public interface UserRepository extends CrudRepository<User, Long> {}

UserController.java

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

    @Autowired
    private UserRepository userRepository;

    @GetMapping
    public List<User> getAllUsers(){
        return (List<User>) userRepository.findAll();
    }
    @GetMapping("/{id}")
    public ResponseEntity<User> getUser(@PathVariable(value="id") long id){
        Optional<User> user = userRepository.findById(id);
    
        if(user.isPresent()) {
            return ResponseEntity.ok().body(user.get());
        }else {
            return ResponseEntity.notFound().build();
        }
    
    }

    @PostMapping
    public User saveUser(@Validated @RequestBody User user) {
        return userRepository.save(user); 
    }
}

So to summarize I'm receiving an empty JSON object from my GetMapping AND PostMapping annotated calls, even while I have valid data in the table (or have submitted valid post data) when I should be receiving back a json object with {id:0, name:"test"}, does anyone know what might be happening?

Edit: It appears as though lombok is not actually injecting getters and setters when I run my application, changing my user.java to

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public long getId() {
        return this.id;
    }


}

and recalling localhost/api/user/0 returns the expected json object, Why isnt lombok injecting these functions into my code properly with the @Data annotation? (eclipse)

1 Answers1

0

So this was happening because lombok was not injecting getters/setters and the ResponseEntity.ok.body(user) could not retrieve the values within the User class.

Apparently according to this stackoverflow answer when using eclipse lombok requires a plugin to properly inject the methods.

So this problem can be fixed by either manually defining the appropriate getters/setters needed or by installing the plugin for eclipse.