spring boot 自定義請求引數解析註解
阿新 • • 發佈:2019-02-16
介紹
一些請求引數, 需要解析成某個自定義的類, 而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)
@Documented
public @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;
@SpringBootApplication
public 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;
@RestController
public 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