http协议 Hyper Text Transfer Protocol 超文本传输协议,规定浏览器与服务器之间数据传输的规则 特点: Tcp 请求响应模型:一次请求一次响应 http协议是无状态的协议:对于事务没有记忆能力,每次请求响应独立 缺点:多次请求之间不能共享数据 优点:速度快 http请求协议(请求数据的格式) 请求行:请求数据第一行(请求方式get 资源路径 协议http1.1) 请求头:第二行开始,格式key,value 请求体:Post请求有,存放请求参数 get请求:请求参数在请求行资源路径中,没有请求体,get请求大小是有限制的 post请求,请求参数在请求体中 http响应格式(响应数据的格式) 响应行:第一行(协议HTTP/1.1 状态码200 描述OK) 1xx:响应中,临时状态码,表示请求已经接收,告诉客户端应该继续请求或者如果已经完成则忽略 2xx:成功 3xx:让客户端重定向 4xx:客户端错误:请求不存在的资源,客户端未被授权,禁止访问等 5xx:服务端错误:程序抛出异常等 响应头:格式key,value 响应体:响应数据 http协议解析 Apache Tomcat web服务器(web容器,servlet容器) 软件程序,对http协议的操作进行封装,提供网上信息浏览服务 请求响应 Tomcat识别Servlet,Tomcat是Servlet容器 前端控制器:DispatcherServlet,springBoot提供,实现Servlet接口 请求数据对象:HttpServletRequest 响应数据对象:HttpServletResponse http-DispatcherServlet(封装HttpServletRequest)-xxxController 插件: lombok 生成pojo方法 dom4j 解析XML文件 json: raw 生的,未处理的数据,即json @ResponseBody 类型:方法注解、类注解 作用:将方法返回值直接响应,如果返回值类型是 实体对象/集合,将转换为json 说明:@RestController=@Controller+@ResponseBody html文件放于static中,public或resource
package com.example.controller;
import com.example.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
@RestController
public class RequestTestOne {
// 传统接收参数方式
@RequestMapping(value = "request", method = RequestMethod.GET)
public String request(HttpServletRequest httpServletRequest) {
final String name = httpServletRequest.getParameter("name");
final String ageStr = httpServletRequest.getParameter("age");
final int age = Integer.parseInt(ageStr);
System.out.println(name + age);
return "OK";
}
// springboot只需形参名与参数名一致,会自动类型转换
@RequestMapping(value = "requestSpring", method = RequestMethod.POST)
// required默认是true,表示必须传值
public String requestSpring(@RequestParam(name = "name", required = false) String username, Integer age) {
System.out.println(username + age);
return "OK";
}
@RequestMapping(value = "pojo", method = RequestMethod.POST)
public String testPojo(User user) {
System.out.println(user);
return "OK";
}
// 复选框参数接收,默认是数组接收,形参名,需要和请求参数名称一致
public void arrayHobby(String[] hobby) {
System.out.println(Arrays.toString(hobby));
}
// 集合参数接收,需配合注解
public void listHobby(@RequestParam List<String> hobby) {
System.out.println(hobby);
}
// 日期类型接收
public void dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime time) {
System.out.println(time);
}
//json数据封装,需要配合@RequestBody
@RequestMapping(value = "jsonParam", method = RequestMethod.POST)
public String jsonParam(@RequestBody User user) {
System.out.println(user);
return "OK";
}
// 路径参数
@RequestMapping("path/{id}/{name}")
public String pathParam(@PathVariable Integer id, @PathVariable String name) {
System.out.println(id + name);
return "OK";
}
}
反射
package com.example.controller;
import com.example.config.ResultVO;
import com.example.pojo.Emp;
import com.example.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmpController {
@RequestMapping("listEmp")
public ResultVO list() {
// 1.加载并解析XML
// final String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
String file="D:\\Users\\JavawebCode\\web二期\\projectFour\\src\\main\\resources\\emp.xml";
System.out.println(file);
final List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
// 2.对数据进行转换处理-gender job
empList.stream().forEach(emp -> {
final String gender = emp.getGender();
if("1".equals(gender)){
emp.setGender("男");
}else if("2".equals(gender)){
emp.setGender("女");
}
});
// 3.响应数据
return new ResultVO(200,"成功",empList);
}
}