我的代码以这种方式工作但是有没有办法将这种情况下需要的Response方法与Spring引导anotations结合起来而不返回整个响应数据?
我猜想带有PostMapping的RestController包括Response返回所有内容。
@RestController
@RequestMapping("/v1")
public class Resource {
@PostMapping(value = "/post")
public Response post(@RequestBody final Data data) {
Response response = null;
try {
validateData(data);
LOG.info("SUCCESS");
response = Response.status(Status.OK).entity("Success").build();
} catch (Exception e) {
LOG.error(e.getMessage(), e.getCause());
response = Response.status(Status.BAD_REQUEST).entity(e.getMessage()).build();
}
return response;
}
{
"context": {
"headers": {},
"entity": "Success",
"entityType": "java.lang.String",
"entityAnnotations": [],
"entityStream": {
"committed": false,
"closed": false
},
"stringHeaders": {},
"mediaType": null,
"allowedMethods": [],
"committed": false,
"entityTag": null,
"links": [],
"acceptableMediaTypes": [
{
"type": "*",
"subtype": "*",
"parameters": {},
"quality": 1000,
"wildcardType": true,
"wildcardSubtype": true
}
],
"acceptableLanguages": [
"*"
],
"entityClass": "java.lang.String",
"requestCookies": {},
"responseCookies": {},
"lengthLong": -1,
"lastModified": null,
"date": null,
"length": -1,
"language": null,
"location": null
},
"status": 200,
"stringHeaders": {},
"statusInfo": "OK",
"mediaType": null,
"metadata": {},
"allowedMethods": [],
"cookies": {},
"entityTag": null,
"links": [],
"lastModified": null,
"entity": "Success",
"date": null,
"length": -1,
"language": null,
"location": null,
"headers": {}
}
如果我使用相同代码的Jersey anotations l得到我需要的东西。身体对我的数据的反应,也不能使用Swagger2,因为不支持泽西岛 有没有办法在没有返回Response方法中的所有内容的情况下使用Spring引导anotations的第一部分,只是状态代码200或400? 方法需要是响应而不是数据或列表,谢谢
@Component
@Path("/v1")
public class Resource {
@POST
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
public Response post(@RequestBody final Data data) {
Response response = null;
try {
validateData(data);
LOG.info("SUCCESS");
response = Response.status(Status.OK).entity(new BasicResponse("0", "Success")).build();
} catch (Exception e) {
LOG.error(e.getMessage(), e.getCause());
response = Response.status(Status.BAD_REQUEST).entity(new BasicResponse(Status.BAD_REQUEST.toString(), e.getMessage())).build();
}
return response;
}
}
答案 0 :(得分:1)
我认为你应该返回executeUpdate
而不是ResponseEntity
,Spring使用Response
代替,ResponseEntity
是Jax-RS类型,所以我怀疑它是否可以正常使用Spring注释,在您的情况下:
Response
应该有效。