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

huangapple go评论92阅读模式
英文:

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

问题

I will provide the translated code parts for you:

Code React 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:

@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";
  }
  // (... Continue similar logic for other variables ...)

  boolean allregion = FilterRegion.isAllregion();
  // (... Continue similar logic for other variables ...)

  // You can return something here based on your business logic
  // For now, there's no return statement in your provided code.
}

Please note that the Java code seems to be missing the return statement or any specific business logic to handle the incoming data, so you may need to add that based on your requirements.

英文:

I'm developing and web application which communicates with my Server. This communication is done thru Spring framework and react js. I'm sending an request for my server to read json but i got this error

error react console :

> Uncaught (in promise) Error: Request failed with status code 400

error spring boot :

> 2020-08-10 11:54:34.550 WARN 22020 --- [nio-8080-exec-3]
> .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
> [org.springframework.http.converter.HttpMessageNotReadableException:
> I/O error while reading input message; nested exception is
> java.io.IOException: Stream closed]

image json :

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

code react 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(&quot;http://localhost:8080/MenuFiltre/filtreregioncloser&quot;,FilterClassification , FilterRegion )
}

Code java spring boot :

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

答案1

得分: 1

@RequestBody注解的参数应该包含整个请求的主体并绑定到一个对象上。

我认为你的参数中不能有多个@RequestBody。

英文:

> @RequestBody annotated parameter is expected to hold the entire body
> of the request and bind to one object

I think u can't have more than one RequestBody in your parameters

答案2

得分: 1

如上所述 - 不能有2个或更多的@RequestBody。我建议创建一个包装类,将从这两个类中获取这些参数。还要摆脱控制器层的这种“逻辑”... 摆脱这些if条件... 考虑使用vavr库的模式匹配,因为策略模式似乎在这里有点过于复杂。

英文:

As mentioned above - you can't have 2 or more @RequestBody. I recommend creating a wrapper class that will hold these parameters from these 2 classes of yours. 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

huangapple
  • 本文由 发表于 2020年8月10日 19:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63339015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定