Could not read JSON: Unexpected end-of-input in field name
Asked Answered
I

3

13

I am developing a Spring MVC web application. I am not still develop the UI. So I am testing my services using Advance Rest Client tool.

My Controller

@Controller
@RequestMapping("/testController")
public class TestController {

@Autowired
private TestService testService;

@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE },  produces = { MediaType.APPLICATION_JSON_VALUE })
public
@ResponseBody void testMethod(@RequestBody TestParam testParam) {

    String tenant = testParam.getTenantCode();
    String testString = tenant + " is the tenant";
}
}

TestParam.java class

public class TestParam {

private String testVar;
private String tenantCode;

public String getTenantCode() {
    return tenantCode;
}

public void setTenantCode(String tenantCode) {
    this.tenantCode = tenantCode;
}

public String getTestVar() {
    return testVar;
}

public void setTestVar(String testVar) {
    this.testVar = testVar;
}
}

I send the request using Advance Rest Client and headers and request link has set correctly.

{"testVar":"Test","tenantCode":"DEMO"}

Request link

http://localhost:8080/myApp/controller/testController/test

It works correctly when TestParam has one veriable. When it becomes two or more it gives an Error and it not hit the testMethod.

exception is com.fasterxml.jackson.core.JsonParseException:  Unexpected end-of-input in field name at [Source:org.apache.catalina.connector.CoyoteInputStream@7b24d498; line: 1, column: 43]
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:181)
at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:173)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:135)

I went throw more articles and I still couldn't find the answer.

Isallobar answered 19/4, 2016 at 13:38 Comment(1)
Your code looks ok. Can you try some other client like 'POSTman' and see if ARC is the one screwing up things.Karrykarst
I
49

Increasing Content-Length: in header works

Isallobar answered 19/4, 2016 at 14:50 Comment(1)
Also, can remove Content-Length: from headerFarmhand
C
2

Whats your json format ? I think json format uses literal \n's as delimiters, please be sure that the JSON actions and sources are not pretty printed.

Chantress answered 19/4, 2016 at 13:50 Comment(0)
D
1

There is an issue in ARC where there is no payload and the response is of a type of json. Parser is throwing error because the string is empty and the response report is crashing.

Dnepropetrovsk answered 20/4, 2016 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.