SpringBoot⾃定义注解接收json参数
SpringBoot如果接受json参数的话需要定义实体类然后使⽤@RequestBody注解,但是如果每个接⼝都创建⼀个实体类的话太⿇烦,因此可以使⽤⾃定义注解的⽅法接收。从⽹上发现了这篇博客,解决了⼀个⼤⼤的疑惑。。转载:
RequestJson
package com.fig;
import java.lang.annotation.*;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestJson {
/**
* 值
*/springboot原理pdf
String value() default "";
/**
* 是否必须
*/
boolean require() default false;
}
RequestJsonHandlerMethodArgumentResolver
package com.fig;
import com.alibaba.fastjson.JSONObject;
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
slf4j.Slf4j;
import org.apachemons.io.IOUtils;
import MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.t.request.NativeWebRequest;
import org.hod.support.HandlerMethodArgumentResolver;
import org.hod.support.ModelAndViewContainer;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import flect.Constructor;
i.ServerException;
@Slf4j
public class RequestJsonHandlerMethodArgumentResolver  implements HandlerMethodArgumentResolver {
/**
* key
*/
private static final String JSON_BODY_KEY = "JSON_BODY_KEY";
@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 {        // 获取jsonNode
JsonNode jsonNode = getJsonBody(webRequest);
RequestJson requestSingleParam = ParameterAnnotation(RequestJson.class);
// 名
String value = determineParamName(parameter, requestSingleParam);
// 结果
JsonNode paramValue = (value);
// 是否必须
boolean require = quire();
if (paramValue == null && require) {
throw new ServerException("parameter[" + value + "]不能为空。");
}
if (paramValue == null) {
return null;
}
Class<?> parameterType = ParameterType();
Constructor<?> constructor = Constructor(String.class);
Object param = null;
try {
param = wInstance(paramValue.asText());
} catch (Exception e) {
<("bind method parameters error", e);
throw new ServerException("parameter[" + value + "] format error for input value[" + String() + "]");
}
return param;
}
private String determineParamName(MethodParameter parameter, RequestJson requestSingleParam) {
String value = requestSingleParam.value();
if (value == null  || value.isEmpty()) {
value = ParameterName();
}
return value;
}
private JsonNode getJsonBody(NativeWebRequest webRequest) throws JsonProcessingException {
// 有就直接获取
String jsonBody = (String) Attribute(JSON_BODY_KEY, NativeWebRequest.SCOPE_REQUEST);
// 没有就从请求中读取
if (jsonBody == null) {
try {
HttpServletRequest servletRequest = NativeRequest(HttpServletRequest.class);
jsonBody = Reader());
webRequest.setAttribute(JSON_BODY_KEY, jsonBody, NativeWebRequest.SCOPE_REQUEST);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return new ObjectMapper().readTree(jsonBody);
}
}
配置config
package com.fig;
import t.annotation.Configuration;
import org.hod.support.HandlerMethodArgumentResolver;
import org.springframework.fig.annotation.ResourceHandlerRegistry;
import org.springframework.fig.annotation.WebMvcConfigurer;
import java.io.File;
import java.util.List;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new RequestJsonHandlerMethodArgumentResolver());
WebMvcConfigurer.super.addArgumentResolvers(resolvers);
}
}
使⽤
public Result register(@RequestJson String userCode, @RequestJson String nickName, @RequestJson String password) {成功

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。