Spring MVC can return data in the JSON format to your front end. You can use jQuery to make ajax requrest to the MVC controller and controller can return the data in json format. Let us see how this can be acheived.
Assuming you are using Maven for dependency management.
Add this dependency to your pom.xml
1
2
3
4
5
6
7
8
9
10
11
<!-- Jackson Dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.0.0</version>
</dependency>
Make sure you have enabled mvc in your configuration as below:
1
2
3
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
In your controller method use the following style of code to return json.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@RequestMapping(value="/testjson.html", method = RequestMethod.GET)
public @ResponseBody User getUserInJSON() {
User user = new User();
user.setActive(false);
user.setEmail("test@test.com");
user.setFirstName("test");
user.setLastName("user");
user.setUserId(10);
return user;
}
@ResponseBody annotations is the most important annotation in the code below. This annotations is making the framework to convert the user object into json format and return to the front-end.
http://localhost:8080/vulab/testjson.html
When you invoke the controller’s method you will get back output in json format as below:
1
{"userId":10,"firstName":"test","lastName":"user","email":"test@test.com","password":null,"active":false,"roles":[],"phone":null,"interests":[]}
For your reference a code snippet from : User.java code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.trainingram.code.domain;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name="user"
, uniqueConstraints = @UniqueConstraint(columnNames="email")
)
public class User implements java.io.Serializable {
private Integer userId;
private String firstName;
private String lastName;
private String email;
private String password;
private boolean active;
private Set<Role> roles = new HashSet<Role>(0);
private Phone phone;
private Set<Interest> interests = new HashSet<Interest>(0);
public User() {
}
public User(String firstName, String lastName, String email, String password, boolean active) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.active = active;
}
public User(String firstName, String lastName, String email, String password, boolean active, Set<Role> roles, Phone phone, Set<Interest> interests) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.active = active;
this.roles = roles;
this.phone = phone;
this.interests = interests;
}
Enjoy your code. Vulab provides handson training in spring framework and other technologies.
Recent Comments