Spring boot - 不支持请求方法'POST'。

时间:2018-01-15 10:16:30

标签: spring-boot crud

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

我的控制器代码

`@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的回应 Postman sample

1 个答案:

答案 0 :(得分:2)

在HTTP请求中发送JSON有效负载时,需要指定值为Content-Type的{​​{1}} HTTP标头。