简体   繁体   English

HttpMessageNotReadableException:无法读取 JSON:使用 Spring 引导和 Android 无法识别的字段

[英]HttpMessageNotReadableException: Could not read JSON: Unrecognized field using Spring boot and Android

I'm developing and web application which communicates with my Server.我正在开发与我的服务器通信的 web 应用程序。 This communication is done thru Spring framework and react js.这种通信是通过 Spring 框架和 react js 完成的。 I'm sending an request for my server to read json but i got this error我正在向我的服务器发送读取 json 的请求,但我收到此错误

error react console:错误反应控制台:

Uncaught (in promise) Error: Request failed with status code 400未捕获(承诺中)错误:请求失败,状态码为 400

error spring boot:错误 spring 启动:

2020-08-10 11:54:34.550 WARN 22020 --- [nio-8080-exec-3].wsmsDefaultHandlerExceptionResolver: Resolved [org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; 2020-08-10 11:54:34.550 WARN 22020 --- [nio-8080-exec-3].wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException:读取输入消息时出现 I/O 错误; nested exception is java.io.IOException: Stream closed]嵌套异常是 java.io.IOException: Stream 关闭]

image json:图片 json:

在此处输入图像描述

code react js:代码反应js:

 constructor(props) {
    super(props);
    this.state = {
      rfp: false,
      rfx: false,
      rfp_x: false,
      allclassification: false,
  
       eu : false,
      americas: false,
      aae: false,
      ger: false,
      eu2: false,
      latam : false,
      empty: false,
      allregion: false,
    }
    
    this.onChange = this.onChange.bind(this);
    this.onSubmit = this.onSubmit.bind(this);
  }

  onChange(e) {
    this.setState({ [e.target.name]: e.target.checked });
  }

  onSubmit(e) {
    e.preventDefault();
    const FilterClassification = {
      // classification
      rfx: this.state.rfx,
      rfp: this.state.rfp,
      rfp_x: this.state.rfp_x,
      allclassification: this.state.allclassification
    };
    const FilterRegion = {
      //Region
     eu : this.state.eu,
    americas  : this.state.americas,
    aae : this.state.aae,
    ger: this.state.ger,
    eu2 : this.state.eu2,
    latam : this.state.latam,
    empty : this.state.empty,
    allregion : this.state.allregion,
    }





console.log(FilterClassification);
    console.log(FilterRegion);
    axios.post("http://localhost:8080/MenuFiltre/filtreregioncloser",FilterClassification , FilterRegion )
  }

Code java spring boot:代码 java spring 启动:

    @PostMapping("/filtreregioncloser")
    public Iterable<Closerfprfx>gettab1(@RequestBody  FilterClassification FilterClassification  , @RequestBody  FilterRegion FilterRegion) 
    {
    boolean rfx = FilterClassification.isRfx();
            String ChaineRfx = "";
            if(rfx==true)
            {
                ChaineRfx="rfx";
            }else
            {
                ChaineRfx="xxxx";
            }
            boolean rfp =FilterClassification.isRfp() ;
            String ChaineRfp = "";
            if(rfp == true)
            {
                ChaineRfp="rfp";
            }else
            {
                ChaineRfp="xxxx";
            }
            boolean rfp_x= FilterClassification.isRfp_x();
            String ChaineRfp_x = "";
            if(rfp_x==true)
            {
                ChaineRfp_x="rfp_x";
            }else
            {
                ChaineRfp_x="xxxx";
            }
            boolean allclassification = FilterClassification.isAllclassification() ;
            
            
               boolean eu = FilterRegion.isEu();
               String ChaineEu= "";
               if(eu==true)
               {
                   ChaineEu="eu";
               }else
               {
                   ChaineEu="xxxx";
               }
               boolean  americas = FilterRegion.isAmericas();
               String ChaineAmericas = "";
               if(americas==true)
               {
                   ChaineAmericas="americas";
               }
               else {
                   ChaineAmericas="xxxx";
               }
               boolean  aae = FilterRegion.isAae();
               String ChaineAae = "";
               if(aae==true)
               {
                   ChaineAae="aae";
               }else {
                   ChaineAae="xxxx";
               }
               boolean  ger = FilterRegion.isGer();
               String ChaineGer="";
               if(ger==true)
               {
                   ChaineGer="ger";
                   
               }else
               {
                   ChaineGer="xxxx";
               }
               boolean  eu2 = FilterRegion.isEu2();
               String ChaineEu2="";
               if(eu2=true)
               {
                   ChaineEu2="eu2";
               }else {
                   ChaineEu2="xxxx";
               }
               boolean  latam = FilterRegion.isLatam() ;
               String ChaineLatam = "";
               if(latam=true)
               {
                   ChaineLatam="latam";
                   }else {
                   ChaineLatam="xxxx";
               }
               boolean  empty = FilterRegion.isEmpty();
               String ChaineEmpty="";
               if(empty=true)
               {
                   ChaineEmpty="empty";
               }else {
                   ChaineEmpty="xxxx";
               }
                boolean  allregion = FilterRegion.isAllregion();
}



 

@RequestBody annotated parameter is expected to hold the entire body of the request and bind to one object @RequestBody 注释参数预计会保存请求的整个正文并绑定到一个 object

I think u can't have more than one RequestBody in your parameters我认为你的参数中不能有多个 RequestBody

As mentioned above - you can't have 2 or more @RequestBody.如上所述 - 您不能拥有 2 个或更多 @RequestBody。 I recommend creating a wrapper class that will hold these parameters from these 2 classes of yours.我建议创建一个包装器 class 来保存您的这两个类的这些参数。 Also get rid of this 'logic' from your controller layer... and get rid of these ifs... consider using pattern matching from vavr library since strategy pattern seems to be overkill there还要从您的 controller 层中摆脱这种“逻辑”……并摆脱这些 ifs……考虑使用 vavr 库中的模式匹配,因为那里的策略模式似乎有点矫枉过正

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HttpMessageNotReadableException:无法读取JSON:读取超时 - HttpMessageNotReadableException: Could not read JSON: Read timed out 对Spring MVC控制器的POST结果为“ HttpMessageNotReadableException:无法读取JSON:未找到合适的构造函数” - POST to Spring MVC controller results in “HttpMessageNotReadableException: Could not read JSON: No suitable constructor found” Spring Boot HttpMessageNotReadableException - Spring Boot HttpMessageNotReadableException 在 Spring Controller 中使用 JSON 补丁时出现 HttpMessageNotReadableException - HttpMessageNotReadableException when Using JSON Patch in Spring Controller HttpMessageNotReadableException:无法读取JSON:无法解析日期值“ I” - HttpMessageNotReadableException: Could not read JSON: Failed to parse Date value 'I' 如何修复Spring引导中发生的&#39;HttpMessageNotReadableException&#39;和&#39;SocketTimeOutException&#39; - How to fix 'HttpMessageNotReadableException' and 'SocketTimeOutException' occured in spring boot 这个错误是什么&#39;org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法反序列化实例&#39;是什么意思? - What does this error 'org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance' mean? HttpMessageNotReadableException:JSON 解析错误:无法识别的令牌“嬀崀” - HttpMessageNotReadableException: JSON parse error: Unrecognized token '嬀崀' 春天HttpMessageNotReadableException - Spring HttpMessageNotReadableException 使用Spring时获取HttpMessageNotReadableException异常-RestTemplate - Get HttpMessageNotReadableException Exception when using Spring - RestTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM