http请求-简单参数
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 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";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user