Spring Boot处理JSON数据

示例【Spring Boot处理JSON数据】

程序清单:/springboot2/src/main/java/com/dwx/hello/User.java

package com.dwx.hello;
public class User {
	private String userName;
	private String sex;
	private Integer age;
	public User() {}
	public User(String userName,String sex,Integer age) {
		this.userName=userName;
		this.sex=sex;
		this.age=age;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [userName=" + userName + ", sex=" + sex + ", age=" + age + "]";
	}

}

程序清单:/springboot2/src/main/resources/templates/index.html





Spring Boot处理JSON数据







	

Spring Boot处理JSON数据

用户名:

性别:

年龄:

程序清单:/springboot2/src/main/java/com/dwx/hello/UserController.java

package com.dwx.hello;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
	@RequestMapping("/findUser")
	public User findUser(@RequestBody User user) {
		System.out.println(user);
		user.setSex("男");
		user.setAge(22);
		return user;
	}
}

启动Spring Boot项目,访问如下地址:http://localhost:8080/

控制台输出

Spring Boot处理JSON数据_第1张图片

页面输出

Spring Boot处理JSON数据_第2张图片

你可能感兴趣的:(spring,boot)