使用Spring MVC创建服务时发生java错误
我正在开发一种休息服务。 它具有以下控制器
@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;
}
}
这是userfoodobservationblocktodto
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;
}
}
然后我们有一个食物观察DTO作为
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;
}
}
但是当我通过邮递员和Params打电话给rest服务时
{
"sessionId": "2",
"foodObservations": [
{
"foodSubType": "1",
"time": "2016-12-15 09:00:00",
"food_serving_size": "3",
"sessionId": "2",
"sessionRecordId": "1"
}
]
}
它说客户端发送的请求在语法上不正确。 如果有人能帮我的话。非常感谢
# 1 楼答案
尝试以下JSON:
原因可能是Spring无法将JSON解析为预期的类对象,因为字段名不同