【问题标题】:Spring boot - Request method 'POST' not supported.Spring boot - 不支持请求方法“POST”。
【发布时间】:2018-01-15 10:16:29
【问题描述】:

我在尝试创建客户时遇到此错误。有人能帮我吗?可能是我错过了一些东西。我什至尝试将@PostMapping 更改为@RequestMapping。谢谢

我的控制器代码

`@PostMapping("CREATE_CUSTOMER_ENDPOINT")
   @ResponseStatus(value = HttpStatus.OK)
   @ApiResponses(value = {
        @ApiResponse(code = 201, message = "The Customer was Created", response = CustomerDto.class),
        @ApiResponse(code = 400, message = "Bad Request", response = ResponseError.class),
        @ApiResponse(code = 500, message = "Unexpected error")
})
  public ResponseEntity createCustomer(final HttpServletRequest request, @RequestBody CustomerDto customerDto)
{
    if (log.isDebugEnabled()){
        log.debug("[CustomerResource] POST {} : Creating customer ", CREATE_CUSTOMER_ENDPOINT);
    }

    if(customerDto.getUidpk()!=null) {
        ResponseError error  = new ResponseError(HttpStatus.BAD_REQUEST.getReasonPhrase(), "A customer Already exist with an Uidpk");
        log.error("[CustomerResource] The customer Already exist ({}) with an Uidpk", customerDto.getUidpk());
        return new ResponseEntity<>(error, null,    HttpStatus.BAD_REQUEST);
    }

    CustomerDto result = customerService.createCustomer(customerDto);
    log.debug("[CustomerResource] Customer created ({})", result.getUidpk());
    return new ResponseEntity<>(result, HeaderUtil.putLocationHeader(request.getRequestURL().toString() + "/" + result.getUidpk()), HttpStatus.CREATED);

} `

我的端点

private static final String CUSTOMER_SEARCH_USER_ID_ENDPOINT = "/customers/{userId:.+}"; private static final String CREATE_CUSTOMER_ENDPOINT= "/customer"; private static final String UPDATE_CUSTOMER_ENDPOINT= "/customer"; private static final String DELETE_CUSTOMER_ENDPOINT = CREATE_CUSTOMER_ENDPOINT + "/{uidpk}";

这是邮递员的回复 Postman sample

【问题讨论】:

  • 它与你的注释有关吗,@PostMapping("CREATE_CUSTOMER_ENDPOINT"),我想你想要这个 --> @PostMapping(CREATE_CUSTOMER_ENDPOINT)
  • 错误对不起。我纠正了它,我在 Postman 中有一个新错误:“错误”:“内部服务器错误”,“error_description”:[“不支持内容类型'text/plain;charset = UTF-8'”]}
  • 在您的请求中添加标头,它会解决它,请使用任何在线验证器验证您的 JSON 值

标签: spring-boot crud


【解决方案1】:

当您在 HTTP 请求中发送 JSON 负载时,您需要指定 Content-Type HTTP 标头,其值为 application/json

【讨论】:

  • 他也应该使用@PostMappingconsumesproduces 成员来达到这个效果。
  • 我的请求运行良好。但是当我最近提到我的 createCustomer 方法时,它进入这种情况 if(customerDto.getUidpk()!=null) 并回复“客户已经存在 Uidpk”。我正在使用 H2 数据库。我的数据库是空的。请我真的很困惑。任何建议谢谢你
猜你喜欢
  • 2018-06-03
  • 2020-03-31
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多