0

I have a java web application with spring boot version 3.1.0

User class is:


import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

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

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "user_id")
    private Integer id;
    
    @Column(name = "username")
    private String username;

    public User() {
    }

    public User(Integer id) {
        this.id = id;
    }
}

UserController is:


import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


@RestController
@RequestMapping("/users")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("")
    public ResponseEntity<List<User>> getUsers() {
        List<User> users = userService.getAllUsers();
        return ResponseEntity.ok(users);
    }

    @GetMapping("/{id}")
    public ResponseEntity<String> getUserById(@PathVariable Integer id)throws JsonProcessingException {
        Optional<User> userOptional = userService.getUserById(id);
        
        if (userOptional.isPresent()) {
            User user = userOptional.get();
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}

UserService is:


import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

   @Autowired
   private UserRepository userRepository;

   public List<User> getAllUsers() {
       return userRepository.findAll();
   }

   public Optional<User> getUserById(Integer id) {
       return userRepository.findById(id);
   }

   public void saveOrUpdateUser(User user) {
       userRepository.save(user);
   }

   public void deleteUser(Integer id) {
       userRepository.deleteById(id);
   }

}

UserRepository is:

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {

    Optional<User> findById(int id);
    
}

When I call http://localhost:8080/xxx/users/1, I get this result:

{
  "username" : "my-email@gmail.com",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/xxx/users/1"
    },
    "user" : {
      "href" : "http://localhost:8080/xxx/users/1"
    }
  }
}

Field id of User class is never returned and I don't understand why. I tried also with

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
@JsonProperty("id")
@JsonInclude(JsonInclude.Include.ALWAYS)

I use lombok lib for getter and setter. However, I tried also to define getter and setter manually in the classic way but nothing changes.

What's wrong with this?

Thanks

Luke
  • 163
  • 4
  • 19

2 Answers2

0

You change return type from string to user object

@GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable Integer id)throws JsonProcessingException {
        Optional<User> userOptional = userService.getUserById(id);
        
        if (userOptional.isPresent()) {
            User user = userOptional.get();
            return ResponseEntity.ok(user);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

}
0

By default the @Id attributes are not rendered. If you want to display the id then you can create a new getter say getId like this :

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

Now it will show id also as part of response.

vijaydeep
  • 395
  • 3
  • 12