javax.ws.rs.core 响应结合 Spring 引导注释 return json 响应中的所有内容?
javax.ws.rs.core Response combine with Spring Boot anotations return everything in json response?
我的代码是这样工作的,但是有没有什么方法可以将我在这种情况下需要的响应方法 与 Spring 启动注释结合起来,而不需要 return 返回整个响应数据?
我猜想带有 PostMapping 的 RestController 包括 Response return everything.
@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 注释,我会得到我需要的。用我的数据在正文中响应,我也不能使用 Swagger2,因为不支持 Jersey。
有没有什么方法可以将第一部分与 spring 引导注释一起使用,而无需 return 响应方法中的所有内容,仅状态代码 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;
}
}
我觉得你应该用returnResponseEntity
代替Response
,Spring用ResponseEntity
代替,Response
是一个Jax-RS 类型,所以我怀疑它能否与 Spring 注释一起正常工作,在你的情况下:
return ResponseEntity.ok("Success");
应该可以。
我的代码是这样工作的,但是有没有什么方法可以将我在这种情况下需要的响应方法 与 Spring 启动注释结合起来,而不需要 return 返回整个响应数据?
我猜想带有 PostMapping 的 RestController 包括 Response return everything.
@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 注释,我会得到我需要的。用我的数据在正文中响应,我也不能使用 Swagger2,因为不支持 Jersey。
有没有什么方法可以将第一部分与 spring 引导注释一起使用,而无需 return 响应方法中的所有内容,仅状态代码 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;
}
}
我觉得你应该用returnResponseEntity
代替Response
,Spring用ResponseEntity
代替,Response
是一个Jax-RS 类型,所以我怀疑它能否与 Spring 注释一起正常工作,在你的情况下:
return ResponseEntity.ok("Success");
应该可以。