http请求-复杂-数组-集合-日期参数
This commit is contained in:
@@ -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<String> 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";
|
||||
}
|
||||
}
|
||||
|
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
# 应用服务 WEB 访问端口
|
||||
server.port=8080
|
||||
|
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>hello word!!!</h1>
|
||||
<p>this is a html page</p>
|
||||
</body>
|
||||
</html>
|
@@ -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() {
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user