我目前正在从头开始制作API,并且在从POSTMAN调用获取响应时遇到问题。我已经查看了几篇关于SO的文章,但找不到解决方案。我的代码如下:
@RestController
@Api(tags="Controller")
public class Controller {
private static final Logger LOGGER = LoggerFactory.getLogger(Controller.class);
@CrossOrigin
@ApiOperation(value = "test")
@RequestMapping(method = RequestMethod.GET, path = "/c000/getpref/v1/test", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiImplicitParams({ @ApiImplicitParam(name = "client-metadata", value = "Client Metadata", required = true, dataType = "string", paramType = "header") })
@ApiResponses(value = { @ApiResponse(code = CinchyConstants.HTTP_RESPONSE_STATUS_SUCCESSFUL_CODE, message = CinchyConstants.HTTP_RESPONSE_STATUS_SUCCESSFUL),
@ApiResponse(code = CinchyConstants.HTTP_RESPONSE_STATUS_BAD_REQUEST_CODE, message = CinchyConstants.HTTP_RESPONSE_STATUS_BAD_REQUEST),
@ApiResponse(code = CinchyConstants.HTTP_RESPONSE_STATUS_NOT_FOUND_CODE, message = CinchyConstants.HTTP_RESPONSE_STATUS_NOT_FOUND),
@ApiResponse(code = CinchyConstants.HTTP_RESPONSE_STATUS_INTERNAL_SERVICE_ERROR_CODE, message = CinchyConstants.HTTP_RESPONSE_STATUS_INTERNAL_SERVICE_ERROR) })
public ResponseEntity<String> test() {
LOGGER.debug("In test()");
String test = "UWU";
return ResponseEntity.ok(test);
}
}
记录器不会打印该语句,而我从POSTMAN获得的信息如下:
{
"timestamp": "2018-09-21T18:07:08.722+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/c000/getpref/v1/test"
}
我尝试使用和不使用标题,但两者都没有为我提供解决方案。有人知道这个问题是什么吗?
答案 0 :(得分:0)
CinchyConstants.HTTP_RESPONSE_STATUS_SUCCESSFUL_CODE是200吗? 另外,如果您希望得到一个响应正文,则应针对每种状态提及它
@ApiResponses(value = {@ApiResponse(code = 200, message = "I have a cool response", response = MyResponse.class),
//......
public ResponseEntity<?> test() {
return new ResponseEntity<MyResponse>(new MyResponse("UWU"), HttpStatus.OK);
}
和MyResponse简单代码:
public class MyResponse{
private String text;
public MyResponse(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
如果使用默认端口启动服务器,则GET到
http://localhost:8080/c000/getpref/v1/test
应以json格式返回您的文本。 为确保您的问题与代码有关,而不与邮递员配置有关,请尝试从浏览器访问链接。