当前位置: 首页 > 知识库问答 >
问题:

在 REST 中测试 POST 请求

许子平
2023-03-14

我使用Postman(chrome扩展)来测试这个REST服务,并且我能够成功地测试GET和DELETE:<code>http://localhost:8080/mt-rest/rest/user/321,但不是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方法中,我从@Request estbody更改为@Model属性,现在我得到了

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}'}

]

共有1个答案

叶阳
2023-03-14

这对我有用,我没有正确设置标题。-H "内容类型:应用程序/json "

$ curl -i -X POST -d '{"version":"1","name":"Himalay","done":"false"} ' http://localhost:8080/mt-rest/rest/user/2/todo -H "Content-Type: application/json"

HTTP/1.1 200 OK Server:Apache-Coyote/1.1 Content-Type:application/JSON Transfer-Encoding:chunked日期:2014年1月23日星期四22:17:34 GMT

========================================================================

 类似资料:
  • 这一条给出: 这个: 我想做的是避免为所有测试编写完整的jsons,如果我可以跳过所有“”和{},将会更快。我的方法正确吗?

  • 测试: 现在我得到了这个错误:

  • 问题内容: 我没有找到解决问题的示例,因此想向你寻求帮助。我不能简单地使用JSON中的RestTemplate对象发送POST请求 每当我得到: 我以这种方式使用RestTemplate: 我怎么了 问题答案: 这项技术对我有用: 我希望这有帮助

  • 我在firefox web Browser中使用Rest Client add on。我想测试一个处理HTTP POST请求并使用JSON的web服务。我如何使用Rest Client测试它? 如果在请求正文中添加json,将得到一个*HTTP 415不受支持的媒体类型错误*。 这样做的正确方法是什么?

  • 我试图从AWS API网关上的REST API调用POST方法。使用curl(用于POST)从命令行正确调用API,使用GET从浏览器正确调用API,因此我知道它工作正常,但我似乎找不到正确的方法使用ConnectionRequest类在codename one上调用POST方法,阅读文档theres addArgument()和setRequestBody(),他们说,它们是独占的,所以我尝试了

  • 我对性能测试和Jmeter非常陌生 这是我的POST数据发送请求POST数据: 回应: 但是当我记录下面的脚本时,它是对上述POST数据的响应(这是正确的) 为什么我的重播失败了?请帮忙!