I have a function like:
@POST @Path("/create") @Produces({MediaType.APPLICATION_JSON}) public Playlist createPlaylist(@FormParam("name") String name) { Playlist p = playlistDao.create(); p.setName(name); playlistDao.insert(p); return p; }
I want the "name" parameter to come from the form OR from the request parameter. If the user is sent to / playlist / create /? Name = bob, then I want it to work. (This mainly helps when testing the API, but also for using it on different platforms without a browser.)
I would like to subclass everything that makes magic binding work ... (@BothParam ("name") String name), but it will need some help to make this happen, since I'm new to Jersey / Java Servlets services.
Update: the next day ...
I solved this by implementing a ContainerRequestFilter that combines form parameters into request parameters. This is not the best solution, but it seems to work. I was not lucky to connect something to the form parameters.
Here's the code in case someone is looking for it:
@Override public ContainerRequest filter(ContainerRequest request) { MultivaluedMap<String, String> qParams = request.getQueryParameters(); Form fParams = request.getFormParameters(); for(String key : fParams.keySet()) { String value = fParams.get(key).get(0); qParams.add(key, value); } }
I would still appreciate it if there was a better way to do this, so I will leave this question open.
source share