将来自nodejs的req.body发送到springboot休息控制器时出现意外的Eof异常
我有一个 Nodejs 应用程序,它从前端接收 json 形式的输入。
输入请求
{
"name":"tina",
"address":{
"doorNo":"54",
"street":"Haddows"
},
"age":"12"
}
我使用express和express validator ->{body,validationresult}来验证json。
在请求中,“地址”是可选的。 如果前端没有传递地址,那么我希望请求采用以下格式:
{ "name":"tina","age": "12"}
Controller.js
const resultant = async
function(req, res) {
try{
console.log(JSON.stringfy(req.body));
const res app.post(
"http://localhost:8080/details",
req.body,req.headers);
} catch(error){
console.log("error is
:"+error.toString())
}
}
RestControllerApp.java
public class RestControllerApp {
@RestController
RequestMapping(value = "/details",
method = RequestMethod.POST)
public ResponseEntity<String>
persist(@RequestBody PersonDTO
person) {
if (personSvc.isValid(person)) {
personRepo.persist(person);
return ResponseEntity.
status(HttpStatus.OK).build();
}
}
}
当请求到达restcontrollerapp.java时,我收到以下错误:
com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (StringReader); line: 1, column: 1]) at [Source: (StringReader); line: 1, column: 8001] at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618) at com.fasterxml.jackson.core.base.ParserBase._handleEOF(ParserBase.java:485) at com.fasterxml.jackson.core.base.ParserBase._eofAsNextChar(ParserBase.java:497) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipWSOrEnd(ReaderBasedJsonParser.java:2340) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser
来自nodejs控制器.js,我在 console.log(JSON.stringfy(req.body)) 中得到以下内容:
{
"name":"tina",
"address":{
"doorNo":"",
"street":""
},
"age":"12"
}
当我将上述 json 粘贴到邮递员中并直接点击 java 控制器时,流程正常工作,而不会显示 jsonException。
任何人都可以请帮我解决这个棘手的问题吗?
提前一百万致谢。
I have a nodejs application which receives input in the form of a json from frontend.
Input request
{
"name":"tina",
"address":{
"doorNo":"54",
"street":"Haddows"
},
"age":"12"
}
I use express and express validator ->{body,validationresult} to validate the json.
In the request the "address" is optional.
If the frontend does not pass the address, then i expect the request to be in the below format:
{ "name":"tina","age": "12"}
Controller.js
const resultant = async
function(req, res) {
try{
console.log(JSON.stringfy(req.body));
const res app.post(
"http://localhost:8080/details",
req.body,req.headers);
} catch(error){
console.log("error is
:"+error.toString())
}
}
RestControllerApp.java
public class RestControllerApp {
@RestController
RequestMapping(value = "/details",
method = RequestMethod.POST)
public ResponseEntity<String>
persist(@RequestBody PersonDTO
person) {
if (personSvc.isValid(person)) {
personRepo.persist(person);
return ResponseEntity.
status(HttpStatus.OK).build();
}
}
}
When the request reaches the restcontrollerapp.java, i get the below error:
com.fasterxml.jackson.core.io.JsonEOFException: Unexpected end-of-input: expected close marker for Object (start marker at [Source: (StringReader); line: 1, column: 1]) at [Source: (StringReader); line: 1, column: 8001] at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidEOF(ParserMinimalBase.java:618) at com.fasterxml.jackson.core.base.ParserBase._handleEOF(ParserBase.java:485) at com.fasterxml.jackson.core.base.ParserBase._eofAsNextChar(ParserBase.java:497) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipWSOrEnd(ReaderBasedJsonParser.java:2340) at com.fasterxml.jackson.core.json.ReaderBasedJsonParser
From the nodejs controller.js, i get the below in console.log(JSON.stringfy(req.body)):
{
"name":"tina",
"address":{
"doorNo":"",
"street":""
},
"age":"12"
}
When i paste the above json in postman and directly hit the java controller, the flow works without showing the jsonexception.
Can anyone please help me out to solve this tricky issue?
A Million thanks in advance.????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论