1.javaee:意為java企業版,指java企業級開發的規范總和,包含13項技術規范
2.事實上服務器和客戶端進行交互的過程中,有一個前端控制器在中間運作,這個控制器為DispatcherServlet,它負責將客戶端請求的信息包裝成HttpServletrequest對象,同時負責將服務器所傳回的響應信息包裝成HttpServletresponse對象.
3.獲取請求參數的方式:
(1)原始方式:
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class ControllerHello {@RequestMapping("/simpleParam")public String simpleParam(HttpServletRequest request){String name = request.getParameter("name");String age = request.getParameter("age");int a = Integer.parseInt(age);System.out.println(name + "+" + age);return "ok";} }
注意點:①getParameter的括號中必須加括號,表示字符串,且必須與請求參數的參數名一致;
②獲取的是個字符串,需要自行進行轉換;
(2)簡單參數:
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class ControllerHello {@RequestMapping("/simpleParam")public String simpleParam(String name, Integer age){System.out.println(name + "+" + age);return "ok";} }
第二種十分簡潔,不需要進行類型轉換;但要求形參名必須和請求參數名一致;
如果不一致,可以加上@RequestParam(name = “name”)注解:
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;@RestController public class ControllerHello {@RequestMapping("/simpleParam")public String simpleParam(@RequestParam(name = "name") String username, Integer age){System.out.println( username + "+" + age);return "ok";} }
同時該注解可以添加第二個參數:required;
默認為true,表示必須傳入,否則報400錯誤,顯示請求異常;
(3)實體對象參數:
這個需要創建一個實體類pojo,并用實體對象來接受請求參數,要求請求參數名必須對應實體類的屬性名;同時有嵌套格式的話需要在請求時,進行加.的修飾:
例如:
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import new_start.new_start4.pojo.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;@RestController public class ControllerHello {@RequestMapping("/pojo")public String pojo(User user){System.out.println(user);return "ok";} }
(4)數組參數:
請求中只需要傳值時使用同一個key值,同時這個key值等同于數組名即可;
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import new_start.new_start4.pojo.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.Arrays;@RestController public class ControllerHello {@RequestMapping("/hobby")public String hobby(String[] hobby){System.out.println(Arrays.toString(hobby));return "ok";} }
同時還可以用集合,只不過要加上@RequestParam:
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import new_start.new_start4.pojo.User; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.Arrays; import java.util.List;@RestController public class ControllerHello {@RequestMapping("/hobby")public String hobby(@RequestParam List<String> hobby){System.out.println(hobby);return "ok";} }
(5)日期參數
(6)json參數:
通過json傳遞參數必須要用post請求方式,要把參數寫在請求體中:同時用實體類接受,實體類的屬性名和鍵名一致:但要注意形參名前要加@RequestBody
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import new_start.new_start4.pojo.User; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import java.util.Arrays; import java.util.List;@RestController public class ControllerHello {@RequestMapping("/json")public String json(@RequestBody User user){System.out.println(user);return "ok";} }(7)路徑參數:注意要加@PathVariable,同時mapping路徑映射要加{}來設定參數名
package new_start.new_start4.controller;import jakarta.servlet.http.HttpServletRequest; import new_start.new_start4.pojo.User; import org.springframework.web.bind.annotation.*;import java.util.Arrays; import java.util.List;@RestController public class ControllerHello {@RequestMapping("/path/{id}")public String json(@PathVariable Integer id){System.out.println(id);return "ok";} }