【Springboot】请求处理、响应处理(请求映射、基本注解、响应JSON、内容协商)

【Springboot】请求处理、响应处理(请求映射、基本注解、响应JSON、内容协商),第1张

【Springboot】请求处理、响应处理(请求映射、基本注解、响应JSON、内容协商)

 学习视频 

尚硅谷雷神SpringBoot2零基础入门springboot全套完整版(spring boot2)

集数:25—42 (原理部分跳过)


 学习笔记 

【Java】学习笔记汇总


文章目录
  • 一、请求处理
    • 1.1 请求映射
    • 1.2 请求映射原理
    • 1.3 基本注解
      •  路径变量 @PathVariable
      •  请求头 @RequestHeader
      •  请求参数 @RequestParam
      •  cookie值 @cookievalue
      •  请求体 @RequestBody
      •  请求域属性 @RequestAttribute
      •  矩阵变量 @MatrixVariable
  • 二、响应处理
    • 2.1 响应JSON
    • 2.2 内容协商

一、请求处理 1.1 请求映射

 @xxxMapping

/getUser   获取用户     
/deleteUser 删除用户    
/editUser  修改用户       
/saveUser 保存用户

 Rest风格支持:使用HTTP请求方式动词来表示对资源的 *** 作

/user   
GET-获取用户    
DELETE-删除用户 
PUT-修改用户 
POST-保存用户  

 Rest风格用法

1 表单method=post,隐藏域 _method=put

2 SpringBoot中手动开启,配置文件

# 开启页面表单的Rest功能
spring:
 mvc:
  hiddenmethod:
   filter:
    enabled: true

 Rest风格原理

请求过来被HiddenHttpMethodFilter拦截,如果请求正常并且为POST,获取到_method的值(兼容PUT.DELETE.PATCH请求,大小写均可)。原生模式的request(post),包装模式requestWrapper重写了getMethod方法,返回的是传入的值。过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。

如果是Android直接发送,或者使用Postman客户端直接发送这些PUT.DELETE.PATCH请求,不会经过filter,所以页面表单的Rest功能可以选择性开启。

@GetMapping("/user") 等同于@RequestMapping(value = "/user",method = RequestMethod.GET)

@RequestMapping(value = "/user",method = RequestMethod.GET)
public String getUser(){
    return "GET-张三";
}

@RequestMapping(value = "/user",method = RequestMethod.POST)
public String saveUser(){
    return "POST-张三";
}


@RequestMapping(value = "/user",method = RequestMethod.PUT)
public String putUser(){
    return "PUT-张三";
}

@RequestMapping(value = "/user",method = RequestMethod.DELETE)
public String deleteUser(){
    return "DELETE-张三";
}

@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}

//自定义filter
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
    HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
    methodFilter.setMethodParam("_m");
    return methodFilter;
}
 

 扩展:如何把_method这个名字换成_m

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.HiddenHttpMethodFilter;

@Configuration(proxyBeanMethods = false)
public class WebConfig {

    @Bean
    public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
        HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
        methodFilter.setMethodParam("_m");
        return methodFilter;
    }
}
1.2 请求映射原理

集数:28

1.3 基本注解

@PathVariable、@RequestHeader、@ModelAttribute、@RequestParam、@MatrixVariable、@cookievalue、@RequestBody

 路径变量 @PathVariable
@RestController
public class ParameterController {

    @GetMapping("/car/{id}/owner/{username}")
    public Map getCar(@PathVariable("id") Integer id,
                                      @PathVariable("username") String name,
                                      @PathVariable Map pv) {
        Map map = new HashMap<>();

        map.put("id", id);
        map.put("name", name);
        map.put("pv", pv);
        return map;
    }
}

访问:localhost:8080/testPathVariable/1/owner/xxx

结果:

{"pv":{"id":"1","username":"xxx"},"name":"xxx","id":1}
 请求头 @RequestHeader
@GetMapping("/testRequestHeader/{id}")
public Map testRequestHeader(@PathVariable("id") Integer id,
                                             @RequestHeader("User-Agent") String userAgent,
                                             @RequestHeader Map pv) {
    Map map = new HashMap<>();
    map.put("id", id);
    map.put("userAgent", userAgent);
    map.put("pv", pv);
    return map;
}

访问:localhost:8080/testRequestHeader/1

结果:

{"pv":{"host":"localhost:8080","connection":"keep-alive","sec-ch-ua":""Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"","sec-ch-ua-mobile":"?0","sec-ch-ua-platform":""Windows"","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1","sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7","cookie":"Idea-11f3a2b2=09893cd1-2e09-40b2-9024-ec951cc04a73; JSESSIonID=FF70BC34EC9A540598FB6B3F29D9C73B; token=19fcec7bda5bef324bdb9af9c6ebf25d"},"userAgent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36","id":1}
 请求参数 @RequestParam
@GetMapping("/testRequestParam")
public Map testRequestParam(@RequestParam("id") Integer id,
                                            @RequestParam("list")List list,
                                            @RequestParam Map pv) {
    Map map = new HashMap<>();
    map.put("id", id);
    map.put("list", list);
    map.put("pv", pv);

    return map;
}

访问:localhost:8080/testRequestParam?id=1&list=l1&list=l2

结果:

{"pv":{"id":"1","list":"l1"},"id":1,"list":["l1","l2"]}
 cookie值 @cookievalue
@GetMapping("/testcookievalue")
public Map testcookievalue(@cookievalue("JSESSIONID") String JSESSIONID,
                                           @cookievalue("JSESSIONID") cookie cookie) {
    Map map = new HashMap<>();
    System.out.println(cookie);
    map.put("JSESSIONID", JSESSIONID);
    return map;
}

访问:localhost:8080/testcookievalue

结果:

{"JSESSIONID":"FF70BC34EC9A540598FB6B3F29D9C73B"}

控制台输出:

javax.servlet.http.cookie@a7fcf7c
 请求体 @RequestBody
@PostMapping("/save")
public Map postMethod(@RequestBody String content) {
    Map map = new HashMap<>();
    map.put("content", content);
    return map;
}

index.html




    
    


  


测试

结果

 请求域属性 @RequestAttribute

通过/goto发送请求,并进行setAttribute,然后将请求转发给/success;在success中获得转发前的请求。

@Controller
public class RequestController {

    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request) {
        request.setAttribute("msg", "成功了...");
        request.setAttribute("code", 200);
        return "forward:/success"; // 转发到 /success请求
    }


    @ResponseBody
    @GetMapping("/success")
    public Map success(@RequestAttribute("msg") String msg,
                       @RequestAttribute("code") Integer code,
                       HttpServletRequest request) {

        Map map = new HashMap<>();
        Object msg1 = request.getAttribute("msg");

        map.put("reqMethod_msg", msg1);
        map.put("annotation_msg", msg);
        return map;
    }
}

访问:localhost:8080/goto

结果:

{"reqMethod_msg":"成功了...","annotation_msg":"成功了..."}
 矩阵变量 @MatrixVariable

配置:

@Configuration(proxyBeanMethods = false)
public class WebConfig{

    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void configurePathMatch(PathMatchConfigurer configurer) {
                UrlPathHelper urlPathHelper = new UrlPathHelper();
                // 不处理";"后面的内容,矩阵变量功能就可以实现了。
                urlPathHelper.setRemoveSemicolonContent(false);
                configurer.setUrlPathHelper(urlPathHelper);
            }
        };
    }
}

案例1

控制器:

@GetMapping("/cars/{path}")
public Map testMatrixVariable(@MatrixVariable("low") Integer low,
                              @MatrixVariable("brand") List brand,
                              @PathVariable("path") String path) {
    Map map = new HashMap<>();
    map.put("low", low);
    map.put("brand", brand);
    map.put("path", path);
    return map;
}

访问:http://localhost:8080/cars/sell;low=34;brand=byd,audi,yd

结果:

{"path":"sell","low":34,"brand":["byd","audi","yd"]}

案例2

两个路径有相同的属性,如何指定获取。

//访问: /boss/1;age=20/2;age=10

@GetMapping("/boss/{bossId}/{empId}")
public Map boss(@MatrixVariable(value = "age",pathVar = "bossId") Integer bossAge,
                @MatrixVariable(value = "age",pathVar = "empId") Integer empAge){
    Map map = new HashMap<>();

    map.put("bossAge",bossAge);
    map.put("empAge",empAge);
    return map;
}
二、响应处理

2.1 响应JSON

所需依赖:


    org.springframework.boot
    spring-boot-starter-web

Web场景自动引入了Json场景


	org.springframework.boot
	spring-boot-starter-json
	2.3.4.RELEASE
	compile

控制类:

@Controller
public class ResponseController {

    @ResponseBody
    @GetMapping("/test/person")
    public Person getPerson() {
        Person person = new Person();
        person.setAge(28);
        person.setBirth(new Date());
        person.setUserName("xxx");
        return person;
    }
}

访问:localhost:8080/test/person

结果:

{"userName":"xxx","age":28,"birth":"2021-11-06T10:31:08.327+00:00"}
2.2 内容协商

根据客户端接收能力不同,返回不同媒体类型的数据。

导入依赖:


    com.fasterxml.jackson.dataformat
    jackson-dataformat-xml

控制类:

@Controller
public class ResponseController {

    @ResponseBody
    @GetMapping("/test/person")
    public Person getPerson() {
        Person person = new Person();
        person.setAge(28);
        person.setBirth(new Date());
        person.setUserName("xxx");
        return person;
    }
}

访问:localhost:8080/test/person

根据客户端请求头中Accept字段,来返回对应的结果。Http协议中规定的,告诉服务器本客户端可以接收的数据类型。如果客户端要接收xml,则返回xml;如果客户端要接收json,则返回json。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5118920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存