diff --git a/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java b/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java new file mode 100644 index 0000000..08b0910 --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java @@ -0,0 +1,13 @@ +package com.inmind; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringbootWebReqRespApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootWebReqRespApplication.class, args); + } + +} 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 new file mode 100644 index 0000000..f7408f3 --- /dev/null +++ b/springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java @@ -0,0 +1,32 @@ +package com.inmind.controller; + +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; + +@RestController +public class RequestController { + + //原始方式接收请求数据 + /*@RequestMapping("/simpleParam") + public String simpleParam(HttpServletRequest request){ + String name = request.getParameter("name"); + String ageStr = request.getParameter("age"); + int age = Integer.parseInt(ageStr); + System.out.println(name+"--"+age); + return "OK"; + }*/ + + //springboot接收方式 + //如果请求参数名与方法的形参名不一致,可以使用@RequestParam(name = "name")来处理 + //@RequestParam(name = "name",required = false):required默认是true,是必须传入的参数,required = false:可以设置参数为非必须 + //01.简单参数 + //02.简单参数-post + @RequestMapping("/simpleParam") + public String simpleParam(@RequestParam(name = "name",required = false) String username, Integer age){ + System.out.println(username+"--"+age); + return "OK"; + } +}