I'm working on a restful web service in Java and I have this method that returns valid JSON:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getJSON()
{
String json = "{\"data\": \"This is my data\"}";
return Response.ok(json).build();
}
The issue I am having is that the JSON is in its string form. How would I send it back in the Object form? In its current state I cannot work with it as soon as my response returns, because the response data is coming back as a string
Just in case here is my web service call from the javascript side
//Use of the promise to get the response
let promise = this.responseGet()
promise.then(
function(response) { //<--- the param response is a string and not an object :(
console.log("Success!", response);
}, function(error) {
console.error("Failed!", error);
}
);
//Response method
responseGet()
{
return new Promise(function(resolve, reject) {
let req = new XMLHttpRequest();
req.open('GET', 'http://localhost:8080/TestWebService/services/test');
req.onload = function() {
if (req.status == 200) {
resolve(req.response);
}
else {
reject(Error(req.statusText));
}
};
req.onerror = function() {
reject(Error("There was some error....."));
};
req.send();
});
}