javaEEday05-请求响应-请求-简单参数
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.inmind.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 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:"+name+",age:"+age);
|
||||
return "OK";
|
||||
}*/
|
||||
|
||||
|
||||
//springboot方式获取请求参数
|
||||
@RequestMapping( "/simpleParam")
|
||||
//@RequestParam(name = "name" ) 的作用是从请求对象中获取name属性值,将值赋给username变量
|
||||
public String simpleParam(@RequestParam(name = "name",required = false ) String username, Integer age){
|
||||
System.out.println("name:"+username+",age:"+age);
|
||||
return "OK";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
spring.application.name=springboot-web-req-resp
|
||||
@@ -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