Commit f9b4ae49be11e6bcfbd5b335de81dfb9a3b2b1ea

Authored by jcartign
1 parent 896af76d

Fix recursion in rest response from UserRestController

src/main/java/fr/plil/sio/web/mvc/Role.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
  3 +import com.fasterxml.jackson.annotation.JsonBackReference;
3 import org.springframework.security.core.GrantedAuthority; 4 import org.springframework.security.core.GrantedAuthority;
4 5
5 import javax.persistence.*; 6 import javax.persistence.*;
@@ -19,6 +20,7 @@ public class Role implements GrantedAuthority { @@ -19,6 +20,7 @@ public class Role implements GrantedAuthority {
19 private String name; 20 private String name;
20 21
21 @ManyToMany 22 @ManyToMany
  23 + @JsonBackReference
22 @JoinTable( 24 @JoinTable(
23 name = "USER_ROLE_T", 25 name = "USER_ROLE_T",
24 joinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID"), 26 joinColumns = @JoinColumn(name = "ROLE_ID", referencedColumnName = "ROLE_ID"),
src/main/java/fr/plil/sio/web/mvc/User.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
3 3
  4 +import com.fasterxml.jackson.annotation.JsonManagedReference;
4 import org.springframework.security.core.GrantedAuthority; 5 import org.springframework.security.core.GrantedAuthority;
5 import org.springframework.security.core.userdetails.UserDetails; 6 import org.springframework.security.core.userdetails.UserDetails;
6 7
@@ -25,6 +26,7 @@ public class User implements UserDetails { @@ -25,6 +26,7 @@ public class User implements UserDetails {
25 private String password; 26 private String password;
26 27
27 @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER) 28 @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER)
  29 + @JsonManagedReference
28 private Set<Role> roles = new TreeSet<>(); 30 private Set<Role> roles = new TreeSet<>();
29 31
30 public Set<Role> getRoles() { 32 public Set<Role> getRoles() {
src/main/java/fr/plil/sio/web/mvc/UserRestController.java
1 package fr.plil.sio.web.mvc; 1 package fr.plil.sio.web.mvc;
2 2
  3 +import org.springframework.web.bind.annotation.PathVariable;
3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RequestMapping;
4 import org.springframework.web.bind.annotation.RequestMethod; 5 import org.springframework.web.bind.annotation.RequestMethod;
5 import org.springframework.web.bind.annotation.RestController; 6 import org.springframework.web.bind.annotation.RestController;
@@ -13,8 +14,13 @@ public class UserRestController { @@ -13,8 +14,13 @@ public class UserRestController {
13 @Resource 14 @Resource
14 private UserService userService; 15 private UserService userService;
15 16
16 - @RequestMapping(value = "/users/", method = RequestMethod.GET) 17 + @RequestMapping(value = "/api/users/", method = RequestMethod.GET)
17 public List<User> listUsers() { 18 public List<User> listUsers() {
18 return userService.findAll(); 19 return userService.findAll();
19 } 20 }
  21 +
  22 + @RequestMapping(value = "/api/users/{username}/", method = RequestMethod.GET)
  23 + public User listUsers(@PathVariable String username) {
  24 + return userService.findByUsername(username);
  25 + }
20 } 26 }
21 \ No newline at end of file 27 \ No newline at end of file