diff --git a/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java b/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java index f7408f3..e31975d 100644 --- a/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java +++ b/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java @@ -1,10 +1,15 @@ package com.inmind.controller; +import com.inmind.pojo.User; +import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.List; @RestController public class RequestController { @@ -29,4 +34,53 @@ public class RequestController { System.out.println(username+"--"+age); return "OK"; } + + /* + 简单实体参数 + */ + @RequestMapping("/simplePojo") + public String simplePojo(User user) { + System.out.println(user); + return "ok"; + } + + + /* + 复杂实体参数 + http://localhost:8080/complexPojo?name=tom&age=23&address.province=江苏&address.city=常州 + */ + @RequestMapping("/complexPojo") + public String complexPojo(User user) { + System.out.println(user); + return "ok"; + } + + /* + 数组参数 + http://localhost:8080/arrayParam?hobby=game&hobby=java&hobby=sing + */ + @RequestMapping("/arrayParam") + public String arrayParam(String[] hobby) { + System.out.println(Arrays.toString(hobby)); + return "ok"; + } + + /* + 集合参数 + http://localhost:8080/arrayParam?hobby=game&hobby=java&hobby=sing + 注意:默认情况下,当前URL的请求参数是由数组接收,如果要改用集合,需要注解@RequestParam + */ + @RequestMapping("/listParam") + public String listParam(@RequestParam List hobby) { + System.out.println(hobby); + return "ok"; + } + + @RequestMapping("/dateParam") + public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){ + System.out.println(updateTime.getYear()); + System.out.println(updateTime.getMonthValue()); + System.out.println(updateTime.getDayOfMonth()); + return "ok"; + } } diff --git a/springboot-web-req-resp/src/main/java/com/inmind/pojo/Address.java b/springboot-web-req-resp/src/main/java/com/inmind/pojo/Address.java new file mode 100644 index 0000000..11b7720 --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/pojo/Address.java @@ -0,0 +1,38 @@ +package com.inmind.pojo; + +public class Address { + private String province; + private String city; + + public Address(String province, String city) { + this.province = province; + this.city = city; + } + + public Address() { + } + + 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 + '\'' + + '}'; + } +} diff --git a/springboot-web-req-resp/src/main/java/com/inmind/pojo/User.java b/springboot-web-req-resp/src/main/java/com/inmind/pojo/User.java new file mode 100644 index 0000000..b316622 --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/pojo/User.java @@ -0,0 +1,48 @@ +package com.inmind.pojo; + +public class User { + private String name; + private Integer age; + private Address address; + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public User(String name, Integer age) { + this.name = name; + this.age = age; + } + + public User() { + } + + @Override + public String toString() { + return "User{" + + "name='" + name + '\'' + + ", age=" + age + + ", address=" + address + + '}'; + } + + 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; + } +} diff --git a/springboot-web-req-resp/src/main/resources/application.properties b/springboot-web-req-resp/src/main/resources/application.properties new file mode 100644 index 0000000..e52b498 --- /dev/null +++ b/springboot-web-req-resp/src/main/resources/application.properties @@ -0,0 +1,3 @@ +# 应用服务 WEB 访问端口 +server.port=8080 + diff --git a/springboot-web-req-resp/src/main/resources/static/index.html b/springboot-web-req-resp/src/main/resources/static/index.html new file mode 100644 index 0000000..89bb8ba --- /dev/null +++ b/springboot-web-req-resp/src/main/resources/static/index.html @@ -0,0 +1,6 @@ + + +

hello word!!!

+

this is a html page

+ + \ No newline at end of file diff --git a/springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java b/springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java new file mode 100644 index 0000000..4b82e01 --- /dev/null +++ b/springboot-web-req-resp/src/test/java/com/inmind/SpringbootWebReqRespApplicationTests.java @@ -0,0 +1,13 @@ +package com.inmind; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringbootWebReqRespApplicationTests { + + @Test + void contextLoads() { + } + +}