1. 程式人生 > >springBoot 自定引數解析器(HandlerMethodArgumentResolver)

springBoot 自定引數解析器(HandlerMethodArgumentResolver)

spring 自定引數解析器(HandlerMethodArgumentResolver)

由於之前用@RequestParam無法接收request payload 正文格式為json格式的字串,只能使用@RequestBody整個接收,覺得麻煩,但是spring自帶的引數解析器不具有這種功能,只能嘗試著用自定義引數解析器去解決。

自定義解析器需要實現HandlerMethodArgumentResolver介面,HandlerMethodArgumentResolver介面包含兩個介面:

介面說明:
supportsParameter:用於判定是否需要處理該引數分解,返回true為需要,並會去呼叫下面的方法resolveArgument。
resolveArgument:真正用於處理引數分解的方法,返回的Object就是controller方法上的形參物件。

1、自定義註解

package com.manqian.crm.resolver;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonParam {

String value();

boolean required() default true;

String defaultValue() default "";

}

若不想自定義註解,可以直接在實現HandlerMethodArgumentResolver的supportsParameter直接返回true

2、自定義引數解析器,實現HandlerMethodArgumentResolver介面

package com.manqian.crm.resolver;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.manqian.crm.api.exception.ParamCheckException;
import org.apache.commons.io.IOUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.MethodParameter;
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;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class JsonPathArgumentResolver implements HandlerMethodArgumentResolver {

private static final String JSON_REQUEST_BODY = "JSON_REQUEST_BODY";

//判斷是否支援要轉換的引數型別
@Override
public boolean supportsParameter(MethodParameter parameter) {
    return parameter.hasParameterAnnotation(JsonParam.class);
}

//當支援後進行相應的轉換
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    String body = getRequestBody(webRequest);
    Object val = null;

    try {
        val = JsonPath.read(body, parameter.getParameterAnnotation(JsonParam.class).value());
        if (parameter.getParameterAnnotation(JsonParam.class).required() && val == null) {
            throw new ParamCheckException(parameter.getParameterAnnotation(JsonParam.class).value() + "不能為空");
        }
    } catch (PathNotFoundException exception) {
        System.out.println(exception.getStackTrace());
        if (parameter.getParameterAnnotation(JsonParam.class).required()) {

// throw new ParamCheckException(parameter.getParameterAnnotation(JsonParam.class).value() + “不能為空”);
throw exception;
}
}
return val;
}

private String getRequestBody(NativeWebRequest webRequest) {
    HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    String jsonBody = (String) servletRequest.getAttribute(JSON_REQUEST_BODY);
    if (jsonBody == null) {
        try {
            jsonBody = IOUtils.toString(servletRequest.getInputStream());
            servletRequest.setAttribute(JSON_REQUEST_BODY, jsonBody);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return jsonBody;

}

}

3、註冊自定義引數解析器
springboot方式:

package com.demo;

import java.util.List;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import com.demo.mvc.component.MultiPersonArgumentResolver;
import com.demo.mvc.component.PersonArgumentResolver;

@SpringBootApplication
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

    // 註冊JsonPathArgumentResolver的引數分解器
    argumentResolvers.add(new JsonPathArgumentResolver());
}

}

package com.manqian.crm.resolver;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.List;

@Configuration
public class ClientResourcesConfig extends WebMvcConfigurerAdapter {

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {

// super.addArgumentResolvers(argumentResolvers);
argumentResolvers.add(new JsonPathArgumentResolver());
}
}

傳統XML格式:

mvc:annotation-driven
mvc:argument-resolvers

</mvc:argument-resolvers>
</mvc:annotation-driven>

使用:
1、依賴:

com.jayway.jsonpath
json-path
2、controller方法使用@JsonParam接收引數

參考:https://stackoverflow.com/questions/12893566/passing-multiple-variables-in-requestbody-to-a-spring-mvc-controller-using-ajax/12897632#12897632

https://my.oschina.net/sugarZone/blog/704575

作者:逆流_Jun
來源:CSDN
原文:https://blog.csdn.net/u010187242/article/details/73647670
版權宣告:本文為博主原創文章,轉載請附上博文連結!