Given a User entity with the following attributes mapped:
@Entity
@Table(name = "user")
public class User {
//...
@Id
@GeneratedValue
@Column(name = "user_id")
private Long id;
@Column(name = "user_email")
private String email;
@Column(name = "user_password")
private String password;
@Column(name = "user_type")
@Enumerated(EnumType.STRING)
private UserType type;
@Column(name = "user_registered_date")
private Timestamp registeredDate;
@Column(name = "user_dob")
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
//...getters and setters
}
I have created a controller method that returns a user by ID.
@RestController
public class UserController {
//...
@RequestMapping(
value = "/api/users/{id}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") Long id) {
User user = userService.findOne(id);
if (user != null) {
return new ResponseEntity<User>(user, HttpStatus.OK);
}
return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
}
//...
}
A service in my business logic layer.
public class UserServiceBean implements UserService {
//...
public User findOne(Long id) {
User user = userRepository.findOne(id);
return user;
}
//...
}
And a repository in my data layer.
public interface UserRepository extends JpaRepository<User, Long> {
}
This works fine, it returns everything about the user, but I use this in several different parts of my application, and have cases when I only want specific fields of the user.
I am learning spring-boot to create web services, and was wondering: Given the current implementation, is there a way of picking the attributes I want to publish in a web service?
If not, what should I change in my implementation to be able to do this?
Thanks.