spring boot 自定义请求参数解析注解
来源:互联网 发布:excel数据透视表命名 编辑:程序博客网 时间:2024/06/02 03:37
介绍
一些请求参数, 需要解析成某个自定义的类, 而spring boot中并没有提供这样自动转换的注解, 但是,spring boot 预留了扩展接口,所以,我们可以自定义实现一个注解.
例如: http://localhost:8999/new/test?a={"name":"aaa", "age":18}&b=123
我们可以通过自定义注解,来把 a 对应的值转换成我们系统中的类
创建注解
package com.example.demo.conf;import java.lang.annotation.*;@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface RequestJson { String value();}
创建请求参数解析实现类
package com.example.demo.conf;import com.alibaba.fastjson.JSONObject;import org.springframework.core.MethodParameter;import org.springframework.stereotype.Component;import org.springframework.web.bind.support.WebDataBinderFactory;import org.springframework.web.context.request.NativeWebRequest;import org.springframework.web.method.support.HandlerMethodArgumentResolver;import org.springframework.web.method.support.ModelAndViewContainer;public class RequestJsonHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.hasParameterAnnotation(RequestJson.class); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { RequestJson requestJson = parameter.getParameterAnnotation(RequestJson.class); String value = requestJson.value(); Class clazz = parameter.getParameterType(); String jsonData = webRequest.getParameter(value); if (jsonData == null) { return clazz.newInstance(); } Object object = JSONObject.parseObject(jsonData, clazz); return object; }}
注入spring boot
package com.example.demo;import com.example.demo.conf.RequestJsonHandlerMethodArgumentResolver;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.transaction.annotation.EnableTransactionManagement;import org.springframework.web.method.support.HandlerMethodArgumentResolver;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.util.List;@SpringBootApplicationpublic class DemoApplication extends WebMvcConfigurerAdapter{ public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(new RequestJsonHandlerMethodArgumentResolver()); super.addArgumentResolvers(argumentResolvers); }}
测试
Controller
package com.example.demo.controller;import com.alibaba.fastjson.JSONObject;import com.example.demo.conf.RequestJson;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MyController { @GetMapping("/new/test") public Object newTest(@RequestJson("a") Person person, @RequestParam("b") Integer b) { System.out.println("a : " + JSONObject.toJSONString(person)); System.out.println("b : " + b); return "ok"; } public static class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }}
请求
请求url: http://localhost:8999/new/test?a={"name":"aaa", "age":18}&b=123
打印结果:
a : {“age”:18,”name”:”aaa”}
b : 123
阅读全文
0 0
- spring boot 自定义请求参数解析注解
- Spring Boot 自定义注解
- spring boot 自定义校验注解
- Spring Aop解析自定义注解
- 学习Spring boot 获取参数的注解
- spring boot注解实现参数校验
- spring boot 自定义注解实现权限验证
- spring boot 常用注解解析
- spring boot 加入jsp自定义标签对get请求参数加密
- Spring自定义参数解析器
- Spring Boot 允许跨域请求、自定义请求头
- Spring 学习默认参数注解解析:
- Spring Mvc获取请求参数和 返回参数和注解
- 构建Spring Restful API请求参数解析
- 构建Spring Restful API请求参数解析
- 构建Spring Restful API请求参数解析
- Spring Boot项目中自定义注解的使用
- Spring Boot项目中自定义注解的使用
- MyBatis系统缓存和自定义缓存
- 阿里云CentOS7.3手动搭建LNMP
- Java 代码小技巧
- Ganglia_简述
- tensorflow-gpu遇到的问题
- spring boot 自定义请求参数解析注解
- mysql--索引基础
- 一般项目开发流程(瀑布模型)
- 关于草根seo网站用户回访率的相关探讨
- WinSCP和SecureCRT使用
- 机器学中的参数估计
- 575. Distribute Candies(bitset!!!)
- [LeetCode] 617.Merge Two Binary Trees
- Android Studio根据Json自动生成JavaBean