我正在开发一项休息服务。 它有以下控制器
@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;
}
}
这是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;
}
}
然后我们有食物观察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的邮递员打电话给休息服务时
{
"sessionId": "2",
"foodObservations": [
{
"foodSubType": "1",
"time": "2016-12-15 09:00:00",
"food_serving_size": "3",
"sessionId": "2",
"sessionRecordId": "1"
}
]
}
它说客户端发送的请求在语法上是不正确的。 如果有人能帮助我。真的很感激。
答案 0 :(得分:1)
尝试以下JSON:
{
"sessionId": "2",
"foodObservations": [
{
"foodSubTypeId": "1",
"time": "2016-12-15 09:00:00",
"food_serving_size": "3",
"sessionId": "2",
"sessionRecordId": "1"
}
]
}
原因可能是Spring无法将您的JSON解析为预期的类对象,因为字段名称不同。