javaEEday05-请求响应-请求-实体参数

This commit is contained in:
2026-05-12 15:06:54 +08:00
parent f26babc445
commit 83f6b32952
3 changed files with 101 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
package com.inmind.controller; package com.inmind.controller;
import com.inmind.pojo.User;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@@ -21,7 +22,8 @@ public class RequestController {
}*/ }*/
//springboot方式获取请求参数 //springboot方式获取请求参数(简单参数)
@RequestMapping( "/simpleParam") @RequestMapping( "/simpleParam")
//@RequestParam(name = "name" ) 的作用是从请求对象中获取name属性值将值赋给username变量 //@RequestParam(name = "name" ) 的作用是从请求对象中获取name属性值将值赋给username变量
public String simpleParam(@RequestParam(name = "name",required = false ) String username, Integer age){ public String simpleParam(@RequestParam(name = "name",required = false ) String username, Integer age){
@@ -29,4 +31,13 @@ public class RequestController {
return "OK"; return "OK";
} }
//实体参数
@RequestMapping( "/simplePojo")
public String simplePojo(User user){
System.out.println(user);
System.out.println("name:"+user.getName()+",age:"+user.getAge());
System.out.println("address:"+user.getAddress());
return "OK";
}
} }

View File

@@ -0,0 +1,39 @@
package com.inmind.pojo;
public class Address {
private String province;
private String city;
public Address() {
}
public Address(String province, String city) {
this.province = province;
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}

View File

@@ -0,0 +1,50 @@
package com.inmind.pojo;
public class User {
private String name;
private Integer age;
private Address address;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}