From f4e73213eabb0c9cf1c92b313417ff91ed606a24 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 16 Sep 2025 15:33:36 +0800 Subject: [PATCH] =?UTF-8?q?http=E8=AF=B7=E6=B1=82-=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringbootWebReqRespApplication.java | 13 ++++++++ .../inmind/controller/RequestController.java | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/SpringbootWebReqRespApplication.java create mode 100644 springboot-web-req-resp/src/main/java/com/inmind/controller/RequestController.java 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"; + } +}