I Have a problem with creating response from my RESTful application in JPA. It's like a simulation of the Filmaffinty or imbd websites.
I only want to return only specifics values from a Movie (id, year, name). Also I want to return the value of the item search (if is a movie, MOVIE, is a tv_show, SERIE). That value isn't an attribut from the entity.
import com.fasterxml.jackson.annotation.JsonProperty;
@Path("/search")
@RequestScoped
public class SearchRESTService extends RESTService {
@EJB
MovieService ms;
@EJB
ListMovieService lms;
@EJB
SeriaService ss;
@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response search(@Context HttpServletRequest req) {
if (!checkLoggedIn(req)) {
throw new WebApplicationException("User not logged.");
}
String name = req.getParameter("n_name");
String typeFilter = req.getParameter("movie");
SearchResult sr = new SearchResult();
sr.movies = ms.getAllMovies();
switch (typeFilter){
case "movie":
sr.movies = ms.getMovieByName(name);
break;
/*case "tv_show":
sr.series = ms.getSeriesByName(name);
break;
*/
}
return buildResponse(sr);
}
static class SearchResult {
@JsonProperty("Movies")
Collection<Movie> movies;
@JsonProperty("List_Movies")
Collection<ListMovie> listMovies;
@JsonProperty("Series")
Collection<Seria> series;
}
}
Is it possible to add a "new value" to a JSON. I mean, I have got the class Movie and I want to return to the JSON the type of the item search. Example: if I search a Movie, the JSON has to return itemType = movie, if it a tv_show, itemType = serie
For example this should be my JSON if I search Titanic:
{
"id": 18926,
"name": "Titanic",
"itemType": "Movie",
"year": 1997
}
Don't modify the JSON - that's just a rendering of the object returned.
Instead, return a different object type, perhaps a SearchResult
object. Create, populate, modify it as you like, then let the JSON library do its thing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.