英文:
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 :
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";
}
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();
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论