1
我正在使用Postman(chrome擴展)來測試此REST服務,並且我能夠成功測試GET和DELETE:http://localhost:8080/mt-rest/rest/user/321
,但不支持POST,即使在提供表單數據之後。 我得到..服務器拒絕了這個請求,因爲請求實體的格式不是所請求方法的請求資源所支持的格式。我在這裏做錯了什麼?在REST中測試POST請求
@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<User> getCustomers() {
return service.getUsers();
}
@RequestMapping(value = "/user/{id}/todo", method = RequestMethod.GET)
@ResponseBody
public List<TodoItem> getTransactions(@PathVariable Long id) {
return getUserInfo(id).getTodoItems();
}
@RequestMapping(value = "/user/{id}/todo", method = RequestMethod.POST)
@ResponseBody
public List<TodoItem> 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);
}
}
更新:
在我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}'}
]
不要過多細節,但我認爲你的POST方法'@RequestMapping(value =「/ user/{id}/todo」'是錯誤定義的。您沒有請求任何內容,但是您正在向Web服務提交。 – Sid