Sanjay Nainani Sanjay Nainani - 3 months ago 9
reST (reStructuredText) Question

Error while creating services using Spring MVC

Well I am developing a rest service.
It has the following controller

@RestController
@RequestMapping(value = "/userFoodObservation")
public class UserFoodObservationController extends CommonController{

QSenseLogger logger = QSenseLogger.getLogger(getClass());

@Autowired
private UserFoodObservationService userFoodObservationService;

@RequestMapping(value = "/logFood", method = RequestMethod.POST)
public Object createUserFoodObservation(HttpServletRequest request,
HttpServletResponse response, @RequestBody UserFoodObservationBlockTO foodObservationBlockTO) throws Exception{
ResponseTO responseTO = new ResponseTO();
responseTO.setSuccess(false);
try{
Long userId = Long.parseLong(request.getHeader("userId"));
String timezoneId = request.getHeader("timezoneId");
boolean returnResult = userFoodObservationService.createUserFoodObservation(userId, timezoneId, foodObservationBlockTO);
if(returnResult){
responseTO.setSuccess(true);
}
}
catch(Exception e)
{
if(e.getMessage().toLowerCase().contains("ConstraintViolationException in userFoodObservationBlock Table".toLowerCase())){
logger.info("Unable to create food Observations because this block had already been synced", e.getMessage());
responseTO.setSuccess(true);
}
else{
logger.error("Error occured while creating food observation ");
}
}
return responseTO;
}
}


And this is the UserFoodObservationBlockTO DTO

public class UserFoodObservationBlockTO implements Serializable{

/**
*
*/
private static final long serialVersionUID = -657264742567486410L;

private Long sessionId;

private List<UserFoodObservationTO> foodObservations;

public Long getSessionId() {
return sessionId;
}

public void setSessionId(Long sessionId) {
this.sessionId = sessionId;
}

public List<UserFoodObservationTO> getFoodObservations() {
return foodObservations;
}

public void setFoodObservations(List<UserFoodObservationTO> foodObservations) {
this.foodObservations = foodObservations;
}



}


And then we have a food observation DTO as

public class UserFoodObservationTO implements Serializable{

/**
*
*/
private static final long serialVersionUID = -4264600862718416440L;

private Long foodSubTypeId;

private String time;

private int food_serving_size;

private Long sessionId;

private Long sessionRecordId;

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public int getFood_serving_size() {
return food_serving_size;
}

public void setFood_serving_size(int food_serving_size) {
this.food_serving_size = food_serving_size;
}

public Long getSessionId() {
return sessionId;
}

public void setSessionId(Long sessionId) {
this.sessionId = sessionId;
}

public Long getSessionRecordId() {
return sessionRecordId;
}

public void setSessionRecordId(Long sessionRecordId) {
this.sessionRecordId = sessionRecordId;
}

public Long getFoodSubTypeId() {
return foodSubTypeId;
}

public void setFoodSubTypeId(Long foodSubTypeId) {
this.foodSubTypeId = foodSubTypeId;
}



}


But when I'm calling the rest service through postman with Params

{
"sessionId": "2",
"foodObservations": [
{
"foodSubType": "1",
"time": "2016-12-15 09:00:00",
"food_serving_size": "3",
"sessionId": "2",
"sessionRecordId": "1"

}
]
}


It says The request sent by the client was syntactically incorrect.
if anyone could help me about it. It would be really appreciated.

Answer

Try the following JSON:

{
   "sessionId": "2",
   "foodObservations": [
       {
       "foodSubTypeId": "1",
       "time": "2016-12-15 09:00:00",
       "food_serving_size": "3",
       "sessionId": "2",
       "sessionRecordId": "1"

       }
   ]
}

The reason might be that Spring can't parse Your JSON into expected class object because the field name differ.

Comments