java rest post请求_在REST中测试POST请求

我使用Postman(chrome扩展)来测试这个REST服务,我能够成功测试GET和DELETE: http://localhost:8080/mt-rest/rest/user/321 ,但不能POST,即使在提供表单数据之后 . 我得到..服务器拒绝了这个请求,因为请求实体的格式不受所请求方法所请求资源的支持 . 我在这做错了什么?

eedc6e15-b893-4b19-becd-57524923953a.jpg

@Controller

@RequestMapping("rest")

public class TestController {![enter image description here][2]

@Autowired

private MultitenantService service;

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)

@ResponseBody

public User getUserInfo(@PathVariable Long id) {

return service.getUser(id);

}

@RequestMapping(value = "/user", method = RequestMethod.GET)

@ResponseBody

public List getCustomers() {

return service.getUsers();

}

@RequestMapping(value = "/user/{id}/todo", method = RequestMethod.GET)

@ResponseBody

public List getTransactions(@PathVariable Long id) {

return getUserInfo(id).getTodoItems();

}

@RequestMapping(value = "/user/{id}/todo", method = RequestMethod.POST)

@ResponseBody

public List addTransaction(@PathVariable Long id, @RequestBody TodoItem todoItem) {

User user = getUserInfo(id);

user.getTodoItems().add(todoItem);

service.save(user);

return user.getTodoItems();

}

@RequestMapping(value = "/user/{id}/todo/{todoId}", method = RequestMethod.DELETE)

@ResponseBody

public User addTransaction(@PathVariable Long id, @PathVariable Long todoId) {

User user = getUserInfo(id);

user.deleteTodo(todoId);

service.save(user);

return getUserInfo(id);

}

}

Update:

在我的POST方法中,我从@RequestBody更改为@ModelAttribute,现在我得到了

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [net.tajzich.mt.domain.TodoItem] during persist time for groups [javax.validation.groups.Default, ]

List of constraint violations:[

ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=name, rootBeanClass=class net.tajzich.mt.domain.TodoItem, messageTemplate='{javax.validation.constraints.NotNull.message}'}

]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值