I have a some code with next layers:
@JsonRootName("users")
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "age")
private Integer age;
@Column(name = "code")
private Integer code;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
public class UsersDao {
@Autowired
public SessionFactory sessionFactory;
public Session session() {
return sessionFactory.getCurrentSession();
}
public List<User> findAll(){
Query<User> query = session().createQuery("from User", User.class);
return query.list();
}
}
public class UsersService {
@Autowired
public UsersDao usersDao;
@Transactional(readOnly = true)
public List<User> findAllUsers(){
return usersDao.findAll();
}
}
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
UsersService usersService;
@RequestMapping(value = "/all", produces = {"application/json"})
@ResponseBody
public List<User> getAllUsers() {
return usersService.findAllUsers();
}
}
When I try a get request localhost:8080/testreactproject/users/all
[{"id":1,"firstName":"John","lastName":"Doe","age":28,"code":916},{"id":2,"firstName":"Jane","lastName":"Doe","age":27,"code":985}]
But I want to get next json with the root element
{"users":[{"id":1,"firstName":"John","lastName":"Doe","age":28,"code":916},{"id":2,"firstName":"Jane","lastName":"Doe","age":27,"code":985}]}
This one of dependecy into my pom.xml file
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
I know that I should create instance of ObjectMapper class and then configure this instance next way
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true)
I tried to add this code in controller-class (method getAllUsers) but it doesn't work for me. What do I do wrong?