Issue
My code works this way but is there any way to combine Response method witch l need in this case with Spring boot anotations without returning whole response data back?
l guess that RestController with PostMapping include 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": {}
}
If l use Jersey anotations with same code l get what l need. Response in body with my data, also l cannot use Swagger2 because doesn't support Jersey.
Is there some way to use first part with spring boot anotations without returning everything in Response method, just status code 200 or 400?
Method need to be Response not Data or List, Thanks
@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;
}
}
Solution
I think you should returnResponseEntity
instead of Response
, Spring uses ResponseEntity
instead, Response
is a Jax-RS type so I doubt it will work properly with Spring annotations, in your case:
return ResponseEntity.ok("Success");
should work.
Answered By - zakaria amine