JAVAweb学习日记(六) 请求响应
一、请求-简单参数&实体参数
原始方式:
package com.itheima.controller;
import org.springframework.web.bind.annotation.RequestMapping;
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方式:
package com.itheima.controller;
import org.springframework.web.bind.annotation.RequestMapping;
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方式
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
System.out.println(name+":"+age);
return "OK";
}
}
请求参数不匹配:
package com.itheima.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 {
//springboot方式
@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name="name",required = false) String uuusername, Integer age){
//传递的参数 required=false 表示当前注解的参数可以不传递 若没有标记flase则该注解函数必须传递否则报错
System.out.println(uuusername+":"+age);
return "OK";
}
}
小结:
实体参数:
简单实体对象:
package com.itheima.controller;
import com.itheima.pojo.User;
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("/simplePojo")
public String simplePojo(User user){
System.out.println(user);
return "OOK";
}
}
复杂实体对象:
package com.itheima.controller;
import com.itheima.pojo.User;
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("/complexPojo")
public String complexPojo(User user){
System.out.println(user);
return "OOKK";
}
}
二、请求-数组集合参数
数组参数:
集合参数:
小结:
三、请求-日期参数
四、请求-Json参数
五、请求-路径参数
六、参数总结
七、请求-请求响应