栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

修改 spring cloud feign 调用方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

修改 spring cloud feign 调用方式

有项目要改成 spring cloud 。本着能少干就少干的原则,就偷了个懒,把spring cloud eign的调用方式改了一下。

直接代码
 

public interface IAopActivityBeanService {

    public String name = "IAopActivityBeanService";

   

    @SuppressWarnings("deprecation")

@RequestMapping(value = "/saveUpdate", method = RequestMethod.POST)  //,produces=MediaType.APPLICATION_JSON_UTF8_VALUE ,consumes=MediaType.APPLICATION_JSON_UTF8_VALUE 

    public ActivityBean saveUpdate(@MyParams(name = "optType")OptType optType, @MyParams(name = "bean")ActivityBean iActivityBean,@MyParams(name = "optUserId") Long optUserId) throws SystemOptServiceException;

    @RequestMapping(value = "/search/pages", method = RequestMethod.POST)

    public SelectPage search(@MyParams(name = "optType") OptType optType, @MyParams(name = "searchBean") ActivityBeanSearchBean searchBean,

        @MyParams(name = "commSearchBean") CommSearchBean commSearchBean, @MyParams(name = "optUserId") Long optUserId) throws SystemOptServiceException;

    @RequestMapping(value = "/search/list", method = RequestMethod.POST)

    public List searchList(@MyParams(name = "optType") OptType optType, @MyParams(name = "searchBean") ActivityBeanSearchBean searchBean,

        @MyParams(name = "commSearchBean") CommSearchBean commSearchBean) throws SystemOptServiceException;

    @RequestMapping(value = "/remove", method = RequestMethod.POST)

    public ActivityBean remove(@MyParams(name = "optType") OptType optType, @MyParams(name = "searchBean") ActivityBeanSearchBean searchBean, @MyParams(name = "optUserId") Long optUserId)

        throws SystemOptServiceException;

    @RequestMapping(value = "/remove/all", method = RequestMethod.POST)

    public List removes(@MyParams(name = "optType") OptType optType, @MyParams(name = "searchBean") ActivityBeanSearchBean searchBean, @MyParams(name = "optUserId") Long optUserId)

        throws SystemOptServiceException;

    @RequestMapping(value = "/get", method = RequestMethod.POST)

    public ActivityBean get(@MyParams(name = "id") Long id) throws SystemOptServiceException;

}

public interface IAopActivityBeanExService extends IAopActivityBeanService {

    @RequestMapping(value = "/search/time", method = RequestMethod.POST)
    public List searchByTime(@MyParams(name = "optType") OptType optType, @MyParams(name = "searchBean") ActivityBeanSearchBean searchBean, @MyParams(name = "optUserId") Long optUserId)
        throws SystemOptServiceException;

}

@FeignClient(name = ActivityFeignParams.feign_service, path = ActivityFeignParams.path_activity)
public interface IActivityBeanFeignServiceClient extends IActivityBeanFeignService {

}

@RestController
@RequestMapping("/activity")
public class ActivityBeanFeignService implements IActivityBeanFeignService {

    @Autowired
    @Qualifier(IAopActivityBeanExService.name)
    private IAopActivityBeanExService service;

    @Override
    public SelectPage search(OptType optType, ActivityBeanSearchBean searchBean, CommSearchBean commSearchBean, Long optUserId) throws SystemOptServiceException {

        SelectPage selectPage = service.search(optType, searchBean, commSearchBean, optUserId);
        return selectPage;

    }

    @Override
    public List searchList(OptType optType, ActivityBeanSearchBean searchBean, CommSearchBean commSearchBean) throws SystemOptServiceException {

        return service.searchList(optType, searchBean, commSearchBean);

    }

    @Override
    public ActivityBean get(Long id) throws SystemOptServiceException {

        ActivityBean iActivityBean = service.get(id);

        return iActivityBean;

    }

    @Override
    public ActivityBean remove(OptType optType, ActivityBeanSearchBean bean, Long optUserId) throws SystemOptServiceException {

        ActivityBean iActivityBean = service.remove(optType, bean, optUserId);

        return iActivityBean;

    }

    @Override
    public List removes(OptType optType, ActivityBeanSearchBean searchBean, Long optUserId) throws SystemOptServiceException {

        return service.removes(optType, searchBean, optUserId);
    }

    @Override
    public ActivityBean saveUpdate(OptType optType, ActivityBean iActivityBean, Long optUserId) throws SystemOptServiceException {

        return service.saveUpdate(optType, iActivityBean, optUserId);
    }

}

configs

1: 

import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cloud.openfeign.thirds.MyParams;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MYCloudRequestParamMethodArgumentResolver implements HandlerMethodArgumentResolver {

    private static  Logger logger = LogManager.getLogger(MYCloudRequestParamMethodArgumentResolver.class);

    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(MyParams.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

        HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);

        Assert.state(servletRequest != null, "No HttpServletRequest");
        JSONObject data = null;
        if (servletRequest.getAttribute("params_value") == null) {
            data = create(servletRequest);
            if (data == null)
                return null;
            servletRequest.setAttribute("params_value", data);
        } else {
            data = (JSONObject)servletRequest.getAttribute("params_value");
        }

        MyParams iFormModel = parameter.getParameterAnnotation(MyParams.class);

        Object value = data.get(iFormModel.name());

        if (value == null) {
            return null;
        }

        if (value instanceof JSONObject) {
            return ((JSONObject)value).toJavaObject(parameter.getParameterType());
        }

        else

        if (value instanceof JSONArray) {
            return ((JSONArray)value).toJavaObject(parameter.getParameterType());
        }

        else

        if (value instanceof Enum) {
            return Enum.valueOf((Class)parameter.getParameterType(), (String)value);
        }

        else {

            Converter iConverter = ConvertUtils.lookup(parameter.getParameterType());
            if (iConverter != null) {
                return iConverter.convert(parameter.getParameterType(), value);

            }

            if (parameter.getParameterType().isEnum()) {

                return Enum.valueOf(((Class)parameter.getParameterType()), value.toString());

            }

            {
                throw new Exception("not support " + parameter.getParameterType() + "  type class !");
            }

        }

    }

    private JSONObject create(HttpServletRequest servletRequest) throws Exception {
       

        ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);
       
        EmptyBodyCheckingHttpInputMessage message = new EmptyBodyCheckingHttpInputMessage(inputMessage);
       
        JSONObject iJSONObject= message.getBodyJSONObject();
       
        return iJSONObject;
               
       

    }

    private static class EmptyBodyCheckingHttpInputMessage implements HttpInputMessage {

        private final HttpHeaders headers;

        @Nullable
        private final InputStream body;

        public EmptyBodyCheckingHttpInputMessage(HttpInputMessage inputMessage) throws IOException {
            this.headers = inputMessage.getHeaders();
            InputStream inputStream = inputMessage.getBody();
            if (inputStream.markSupported()) {
                inputStream.mark(1);
                this.body = (inputStream.read() != -1 ? inputStream : null);
                inputStream.reset();
            } else {
                PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream);
                int b = pushbackInputStream.read();
                if (b == -1) {
                    this.body = null;
                } else {
                    this.body = pushbackInputStream;
                    pushbackInputStream.unread(b);
                }
            }
        }

        @Override
        public HttpHeaders getHeaders() {
            return this.headers;
        }

        @Override
        public InputStream getBody() {
            return (this.body != null ? this.body : StreamUtils.emptyInput());
        }

        public boolean hasBody() {
            return (this.body != null);
        }
       
       
        public JSONObject getBodyJSONObject() throws Exception{
            if (hasBody()) {
                InputStream iInputStream = getBody();
                try {

                    byte[] data = new byte[iInputStream.available()];
                    iInputStream.read(data);
                    JSONObject result = JSON.parseObject(new String(data,"utf-8"));
                    return result;
                } finally {
                    if (iInputStream != null) {
                        try {
                            iInputStream.close();
                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                        }
                    }
                }
            } else {
                return null;
            }
        }
    }

}


2:  @SuppressWarnings("deprecation")
@Configuration
public class FeignWebMvcConfigurerAdapter implements WebMvcConfigurer {

  
    @Override
    public void addArgumentResolvers(List resolvers) {
        resolvers.add(new MYCloudRequestParamMethodArgumentResolver());

    }

}

pom.xml

 

jitpack.io

https://jitpack.io

       

com.github.pzzy2000.spring-cloud-openfeign

spring-cloud-starter-openfeign

v2.2.6.RELEASE-myprams-0.0.1

io.github.openfeign

feign-core

   
           
该功能只适用  2.2.6.RELEASE。
 2.2.6.RELEASE

  哈哈 只要在原来的基础上套一层,搞定.

有空了就写个DEMO 了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号