【发布时间】: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