I’m having some trouble with transfering data from and to my web service from my web application.
So i have a Java JAX-RS web service with Spring, Hibernate and Jackson. So far i’ve used JSON-P to get the data from the web service but now I need to do POST calls so i’m trying to switch to CORS.
The old code using JSON-P:
@GET
@Path("/getUsers")
@Produces(MediaType.APPLICATION_JSON)
public ResponseEntity getUsers(@QueryParam("callback") String callback) {
Set<User> list = null;
list = userBo.getUsers();
if (list != null) {
return callback + "(" + list.toString() + ");";
} else {
return null;
}
}
I’ve been trying to change the headers of the response by doing:
@GET
@Path("/getUsers")
@Produces(MediaType.APPLICATION_JSON)
public ResponseEntity getUsers(@QueryParam("callback") String callback) {
Set<User> list = null;
list = userBo.getUsers();
HttpHeaders headers = new HttpHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, OPTIONS, POST");
headers.add("Access-Control-Allow-Headers", "Content-Type");
return new ResponseEntity(list.toString(), headers, HttpStatus.OK);
}
But Jackson converts the entire ResponseEntity to a JSON string. Anyone know how I can change the header without having jackson convert it all?
thanks!
use
Responseinstead ofResponseEntity.