一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring Cloud中使用Feign,@RequestBody無法繼承的解決方案

Spring Cloud中使用Feign,@RequestBody無法繼承的解決方案

2022-02-24 13:46phoebechen_gz Java教程

這篇文章主要介紹了Spring Cloud中使用Feign,@RequestBody無法繼承的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

使用Feign,@RequestBody無法繼承的問題

根據官網FeignClient的例子,編寫一個簡單的updateUser接口,定義如下

?
1
2
3
4
5
6
7
@RequestMapping("/user")
public interface UserService {
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    UserDTO findUserById(@PathVariable("userId") Integer userId);
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    boolean updateUser(@RequestBody UserDTO user);
}

實現類

?
1
2
3
4
5
6
@Override
   public boolean updateUser(UserDTO user)
   {  
       LOGGER.info("===updateUser, id = " + user.getId() + " ,name= " + user.getUsername());
       return false;
   }

執行單元測試,發現沒有獲取到預期的輸入參數

2018-09-07 15:35:38,558 [http-nio-8091-exec-5] INFO [com.springboot.user.controller.UserController] {} - ===updateUser, id = null ,name= null

原因分析

SpringMVC中使用RequestResponseBodyMethodProcessor類進行入參、出參的解析。以下方法根據參數是否有@RequestBody注解判斷是否進行消息體的轉換。

?
1
2
3
4
@Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(RequestBody.class);
    }

解決方案

既然MVC使用RequestResponseBodyMethodProcessor進行參數解析,可以實現一個定制化的Processor,修改supportParameter的判斷方法。

?
1
2
3
4
5
6
7
8
9
@Override
   public boolean supportsParameter(MethodParameter parameter)
   {
       //springcloud的接口入參沒有寫@RequestBody,并且是自定義類型對象 也按JSON解析
       if (AnnotatedElementUtils.hasAnnotation(parameter.getContainingClass(), FeignClient.class) && isCustomizedType(parameter.getParameterType())) {
           return true;
       }
       return super.supportsParameter(parameter);
   }

此處的判斷邏輯可以根據實際框架進行定義,目的是判斷到為Spring Cloud定義的接口,并且是自定義對象時,使用@RequestBody相同的內容轉換器。

實現定制化的Processor后,還需要讓自定義的配置生效,有兩種方案可選:

  • 直接替換RequestResponseBodyMethodProcessor,在SpringBoot下需要自定義RequestMappingHandlerAdapter。
  • 實現WebMvcConfigurer中的addArgumentResolvers接口

這里采用較為簡單的第二種方式,初始化時的消息轉換器根據需要進行加載:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class XXXWebMvcConfig implements WebMvcConfigurer
{
@Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers)
    {
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        stringHttpMessageConverter.setWriteAcceptCharset(false);
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(5);
        messageConverters.add(new ByteArrayHttpMessageConverter());
        messageConverters.add(stringHttpMessageConverter);
        messageConverters.add(new SourceHttpMessageConverter<>());
        messageConverters.add(new AllEncompassingFormHttpMessageConverter());
        CustomizedMappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new CustomizedMappingJackson2HttpMessageConverter();
        jackson2HttpMessageConverter.setObjectMapper(defaultObjectMapper());
        messageConverters.add(jackson2HttpMessageConverter);
        ViomiMvcRequestResponseBodyMethodProcessor resolver = new ViomiMvcRequestResponseBodyMethodProcessor(messageConverters);
        resolvers.add(resolver);
    }

修改完成后,微服務的實現類即可去掉@RequestBody注解。

使用feign遇到的問題

spring cloud 使用feign 項目的搭建 在這里就不寫了,本文主要講解在使用過程中遇到的問題以及解決辦法

1、示例

?
1
2
@RequestMapping(value = "/generate/password", method = RequestMethod.POST)
KeyResponse generatePassword(@RequestBody String passwordSeed);

在這里 只能使用 @RequestMapping(value = "/generate/password", method = RequestMethod.POST) 注解 不能使用

@PostMapping 否則項目啟動會報

Caused by: java.lang.IllegalStateException: Method generatePassword not annotated with HTTP method type (ex. GET, POST) 異常

2、首次訪問超時問題

原因:Hystrix默認的超時時間是1秒,如果超過這個時間尚未響應,將會進入fallback代碼。

而首次請求往往會比較慢(因為Spring的懶加載機制,要實例化一些類),這個響應時間可能就大于1秒了。

解決方法:

<1:配置Hystrix的超時時間改為5秒

?
1
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

<2:禁用Hystrix的超時時間

?
1
hystrix.command.default.execution.timeout.enabled: false

<3:禁用feign的hystrix 功能

?
1
feign.hystrix.enabled: false

注:個人推薦 第一 或者第二種 方法

3、FeignClient接口中

如果使用到@PathVariable,必須指定其value

spring cloud feign 使用 Apache HttpClient

問題:1 沒有指定 Content-Type 是情況下 會出現如下異常 ? 這里很納悶?

Caused by: java.lang.IllegalArgumentException: MIME type may not contain reserved characters

在這里有興趣的朋友可以去研究下源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ApacheHttpClient.class
  private ContentType getContentType(Request request) {
    ContentType contentType = ContentType.DEFAULT_TEXT;
    for (Map.Entry<String, Collection<String>> entry : request.headers().entrySet())
    // 這里會判斷 如果沒有指定 Content-Type 屬性 就使用上面默認的 text/plain; charset=ISO-8859-1
    // 問題出在默認的 contentType : 格式 text/plain; charset=ISO-8859-1
    // 轉到 ContentType.create(entry.getValue().iterator().next(), request.charset()); 方法中看
    if (entry.getKey().equalsIgnoreCase("Content-Type")) {
      Collection values = entry.getValue();
      if (values != null && !values.isEmpty()) {
        contentType = ContentType.create(entry.getValue().iterator().next(), request.charset());
        break;
      }
    }
    return contentType;
  }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ContentType.class
   public static ContentType create(final String mimeType, final Charset charset) {
        final String normalizedMimeType = Args.notBlank(mimeType, "MIME type").toLowerCase(Locale.ROOT);
 // 問題在這 check  中 valid f方法中
        Args.check(valid(normalizedMimeType), "MIME type may not contain reserved characters");
        return new ContentType(normalizedMimeType, charset);
    }
   private static boolean valid(final String s) {
        for (int i = 0; i < s.length(); i++) {
            final char ch = s.charAt(i);
     // 這里 在上面 text/plain;charset=UTF-8 中出現了 分號 導致檢驗沒有通過
            if (ch == '"' || ch == ',' || ch == ';') {
                return false;
            }
        }
        return true;
    }

解決辦法 :

?
1
@RequestMapping(value = "/generate/password", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)

注解中指定: Content-Type 即 指定 consumes 的屬性值 : 這里 consumes 屬性的值在這不做具體講解,有興趣的可以去研究下

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/phoebechen_gz/article/details/82500904

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄漫在线播放 | x8x8在线永久免费观看 | 久99视频精品免费观看福利 | 深夜免费在线视频 | 久久毛片网站 | 9420高清完整版在线观看国语 | 我要色色网 | 99精品视频在线观看免费 | 国产日韩欧美在线播放 | 好湿好紧太硬了我太爽了网站 | 色综合合久久天天综合绕视看 | 欧美一级片观看 | 午夜影院免费观看视频 | 成人快手破解版 | 欧美视| zol中关村在线 | 全是女性放屁角色的手游 | 国产精品二区高清在线 | 四虎国产成人免费观看 | 香蕉tv国产在线永久播放 | 免费在线看片网站 | 亚洲激情在线 | 欧美性受xxxx88喷潮 | 精品无人区麻豆乱码无限制 | 国产亚洲综合久久 | 成人快插 | 日本高清全集免费观看 | 1024免费观看完整版在线播放 | 亚洲国产日韩制服在线观看 | 青青在线视频观看 | 97国产自拍 | 91嫩草私人成人亚洲影院 | 国产在线视频色综合 | 91无套极品外围在线播放 | 青青国产精品 | 91在线精品国产 | 成人观看免费大片在线观看 | 久久草福利自拍视频在线观看 | 天使萌痴汉在线中文字幕 | 欧美整片完整片视频在线 | 国产亚洲一级精品久久 |