一、请求服务器端返回的简单字符串
1、创建获取字符串控制器 - GetStringController
package net.tjl.spring.contorller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class GetStringController {
@RequestMapping(value = "/getString" , produces = "text/html;charset=utf-8")
@ResponseBody
public String getString(){
return "<h1>Spring MVC真有意思!</h1>";
}
}
运行效果
二、请求服务器端返回的JSON数据
1、创建用户实体类 - User
package net.tjl.spring.contorller.bean;
import java.util.Date;
public class User {
private int id;
private String username;
private String password;
private String telephone;
private Date registerTime;
private int popedom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getPopedom() {
return popedom;
}
public void setPopedom(int popedom) {
this.popedom = popedom;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
", popedom=" + popedom +
'}';
}
}
2、创建获取JSON控制器 - GetJsonController
package net.tjl.spring.contorller;
import net.tjl.spring.contorller.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
public class GetJsonController {
@RequestMapping(value = "/getJson", produces = "application/json; charset=utf-8")
public @ResponseBody
User getJson() {
User user = new User();
user.setId(1);
user.setUsername("萌萌哒");
user.setPassword("12345");
user.setTelephone("15889895678");
user.setRegisterTime(new Date());
user.setPopedom(1);
return user;
}
@RequestMapping(value = "/getJsonArray", produces = "application/json; charset=utf-8")
public @ResponseBody
List<User> getJsonArray() {
List<User> users = new ArrayList<User>();
User user = new User();
user.setId(1);
user.setUsername("萌萌哒");
user.setPassword("12345");
user.setTelephone("15889895678");
user.setRegisterTime(new Date());
user.setPopedom(1);
users.add(user);
user = new User();
user.setId(2);
user.setUsername("李文霞");
user.setPassword("45678");
user.setTelephone("13978783456");
user.setRegisterTime(new Date());
user.setPopedom(1);
users.add(user);
user = new User();
user.setId(3);
user.setUsername("郑智化");
user.setPassword("88888");
user.setTelephone("15890903456");
user.setRegisterTime(new Date());
user.setPopedom(1);
users.add(user);
return users;
}
}
3、在pom.xml文件里添加对json的支持
<!--对json的支持-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.7</version>
</dependency>
4、启动服务器,查看结果