SearchContextHandlerMethodArgumentResolver.java 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
package cn.ibizlab.businesscentral.util.web;

import cn.ibizlab.businesscentral.util.filter.SearchContextBase;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
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 java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class SearchContextHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {

    private static ObjectMapper objectMapper=new ObjectMapper();

	@Override
	public boolean supportsParameter(MethodParameter parameter) {
		return SearchContextBase.class.isAssignableFrom(parameter.getParameterType());
	}

	@Override
	public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
								  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
		Map<String, String[]> params = webRequest.getParameterMap();
		LinkedHashMap<String,Object> set=new LinkedHashMap<>();
		for (String key : params.keySet()) {
			set.put(key,params.get(key)[0]);
		}
		String json=objectMapper.writeValueAsString(set);
		return objectMapper.readValue(json,parameter.getParameterType());
	}



}