Map multiple query parameters in jax-rs - rest

I am using Jersey as the implementation library.
#Path("books")
public class Sample {
#GET
public List<Book> getBooks(#Context UriInfo uriInfo)
{
MultivaluedMap<String,String> params = uriInfo.getQueryParameters();
String pageStart = params.getFirst("p");
String pageSize = params.getFirst("s");
}
}
It works fine with /books but doesn't work for /books?p=1&s=10 (http 404 error)
I don't want to define my path like "books?p={p}&s={s}" since there will be dynamic parameters that cannot be predefined.
question:
Is there any way that i can map /books?p=1&s=10 to the getBooks method??

can check by having you #Path("books") changed to #Path("/books")

Related

How to search for multiply(batch) params in Spring boot?

I would like to batch search for entities like that:
GET api/stuff?ids=123+456+789+101112+...
I have found out that it is possible to get the request param like that:
#RequestMapping(method = RequestMethod.GET, value = "/stuff")
public String controllerMethod(#RequestParam Map<String, String> customQuery) {
//After that I could get 123+456+789+101112+... and I could parse them.
String ids = customQuery.get("ids");
}
Is there an alternative for the above solution which I could get the request param as a List, or any other solution?
#GetMapping("/stuff")
public String controllerMethod(#RequestParam("ids") List<Integer> ids) {
}
Then you should be able to call this as api/stuff?ids=123,456,789 or as api/stuff?ids=123&ids=456&ids=678

How to make the #RestController do not response data as restful? [duplicate]

I have a REST endpoint implemented with Spring MVC #RestController. Sometime, depends on input parameters in my controller I need to send http redirect on client.
Is it possible with Spring MVC #RestController and if so, could you please show an example ?
Add an HttpServletResponse parameter to your Handler Method then call response.sendRedirect("some-url");
Something like:
#RestController
public class FooController {
#RequestMapping("/foo")
void handleFoo(HttpServletResponse response) throws IOException {
response.sendRedirect("some-url");
}
}
To avoid any direct dependency on HttpServletRequest or HttpServletResponse I suggest a "pure Spring" implementation returning a ResponseEntity like this:
HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create(newUrl));
return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);
If your method always returns a redirect, use ResponseEntity<Void>, otherwise whatever is returned normally as generic type.
Came across this question and was surprised that no-one mentioned RedirectView. I have just tested it, and you can solve this in a clean 100% spring way with:
#RestController
public class FooController {
#RequestMapping("/foo")
public RedirectView handleFoo() {
return new RedirectView("some-url");
}
}
redirect means http code 302, which means Found in springMVC.
Here is an util method, which could be placed in some kind of BaseController:
protected ResponseEntity found(HttpServletResponse response, String url) throws IOException { // 302, found, redirect,
response.sendRedirect(url);
return null;
}
But sometimes might want to return http code 301 instead, which means moved permanently.
In that case, here is the util method:
protected ResponseEntity movedPermanently(HttpServletResponse response, String url) { // 301, moved permanently,
return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).header(HttpHeaders.LOCATION, url).build();
}
As the redirections are usually needed in a not-straightforward path, I think throwing an exception and handling it later is my favourite solution.
Using a ControllerAdvice
#ControllerAdvice
public class RestResponseEntityExceptionHandler
extends ResponseEntityExceptionHandler {
#ExceptionHandler(value = {
NotLoggedInException.class
})
protected ResponseEntity<Object> handleNotLoggedIn(
final NotLoggedInException ex, final WebRequest request
) {
final String bodyOfResponse = ex.getMessage();
final HttpHeaders headers = new HttpHeaders();
headers.add("Location", ex.getRedirectUri());
return handleExceptionInternal(
ex, bodyOfResponse,
headers, HttpStatus.FOUND, request
);
}
}
The exception class in my case:
#Getter
public class NotLoggedInException extends RuntimeException {
private static final long serialVersionUID = -4900004519786666447L;
String redirectUri;
public NotLoggedInException(final String message, final String uri) {
super(message);
redirectUri = uri;
}
}
And I trigger it like this:
if (null == remoteUser)
throw new NotLoggedInException("please log in", LOGIN_URL);
if you #RestController returns an String you can use something like this
return "redirect:/other/controller/";
and this kind of redirect is only for GET request, if you want to use other type of request use HttpServletResponse

how to get absoluteURIbuilder to return newly created resource in loaction header in JAX-RS?

Consider the following example:
create new user
POST request for this url : www.example.com/accounts/checking/users
GET user detail
GET request for this url : www.example.com/accounts/checking/user/1
return resource for user with id 1.
Now my question is
when I post to www.example.com/accounts/checking/users for a new user creation,
a new user is created and its resource uri must be returned in the location header. I am having trouble building this uri using getAbsoluteURIbuilder method.
Here is what I have
#Path("/accounts)
public class AccountResourceService {
#Context
UriInfo uriInfo
//here 'type' can be checking or saving account
#GET
#PATH("{type}/user/{id}")
#Produces(MediaType.APPLICATION_JSON)
public class getUserInfo(final #PathParam("type") String type, #PathParam("id"), int id)
{
//return user-specific resource for the given id
}
#POST
#PATH("{type}/users")
#Produces(MediaType.APPLICATION_JSON)
public class CreateUser(final #PathParam("type") String type, AccountCreateRequest request)
{
if type.equals("checking"){
User user = createAccount(request);
URI uri = uriInfo.getAbsolutePathBuilder().path(user.getId().toString()).build();
System.out.println("created uri is " + uri.toString);
return Response.created(uri).build();
}
else {
//do something else
}
}
}
The uri I am returning in POST method above is
http://localhost:8080/accounts/checking/users/1 //note users
But the expected uri is
http://localhost:8080/accounts/checking/user/1 //not user
how can I get this to work?
Just to put all the comments into an answer
From a design perspective, I say you just get rid of the user resource path, and make it users/{id}. This is common and an accepted way. users is a collection resource. When you go to /users, you get the collection of users. When you go to users/234, you get the user with id 234, in that collection of users.
But sine you can change this, you can just build the uri with some string manipulation. Get the getAbsolutePath() from the UriInfo, then just remove the s. Then you can create a new UriBuilder with that string. As seen in the below example
#Path("/accounts")
public class AccountsResource {
#POST
#Path("/{type}/users")
#Consumes(MediaType.APPLICATION_JSON)
public Response createUser(#PathParam("type") String type,
#Context UriInfo uriInfo) {
String newUri = uriInfo.getAbsolutePath().toString();
newUri = newUri.substring(0, newUri.length() - 1);
UriBuilder builder = UriBuilder.fromPath(newUri);
URI uri = builder.path("1234").build();
return Response.created(uri).build();
}
}
This will return /accounts/checking/user/1234

JERSEY - Accessing Generic List in Response

Im facing isssue in getting Jersey Generic List in client response. I need to get it as Entity for some reason.
#XmlRootElement(name="list")
#XmlSeeAlso({RESTDomain.class})
public class JAXBContainer<T> {
private List<T> items = new ArrayList<T>();
public JAXBContainer() { }
public JAXBContainer(List<T> items) {
this.items = items;
}
#XmlElementWrapper(name="items")
#XmlAnyElement(lax=true)
public List<T> getItems() {
return items;
}
public void setItems(List<T> items) {
this.items = items;
}
#XmlAttribute
public int getItemsSize() {
return this.items.size();
}
above is my Generic List to the resopnse
#GET
#Produces({MediaType.APPLICATION_XML})
public Response getREST(){
RESTDomain domain = new RESTDomain();
domain.setName("Adams");
domain.setPlace("Zurich");
List<RESTDomain> restDomains = new ArrayList<RESTDomain>();
restDomains.add(domain);
JAXBContainer<RESTDomain> jAXBContainer= new JAXBContainer<RESTDomain>(restDomains);
GenericEntity<JAXBContainer<RESTDomain>> genericEntity = new GenericEntity<JAXBContainer<RESTDomain>>(jAXBContainer){};
return Response.ok(genericEntity).build();
}
Im returning the container with genericEntity. I know with just List inside genericEntity i can get my Entity at my client but the problem is i need to Use my JAXBContainer for some reason.
#Test
public void restGet() throws JAXBException{
ClientConfig cc = new DefaultClientConfig();
client = Client.create(cc);
String baseURI ="http://localhost:3555/SampleREST/rest/sample";
WebResource webResource = client.resource(baseURI);
JAXBContainer<RESTDomain> jAXBContainer= webResource.get(new GenericType<JAXBContainer<RESTDomain>>(){});
System.out.println("response:: "+jAXBContainer.getItemsSize());
}
My problem is im getting the response as JAXBContainer with GenericType as desired but the size of container is 0. What am i missing here? do i have to Use any marshalling and unmarshalling Mechanisms.
But When i request this URI in browser i get the well formed XML, But it fails in client or do we have any other ways to extract entity in client. Thanks in advance
I don't see that you're setting the accept content type anywhere on the client.
Try with: webResource.accept("application/xml")

Pass a parameter to REST web service via URL

I'm creating a small REST web service using Netbeans. This is my code:
private UriInfo context;
private String name;
public GenericResource() {
}
#GET
#Produces("text/html")
public String getHtml() {
//TODO return proper representation object
return "Hello "+ name;
}
#PUT
#Consumes("text/html")
public void putHtml(String name) {
this.name = name;
}
I'm calling the get method ok since when I call http://localhost:8080/RestWebApp/resources/greeting I get "Hello null" but I'm trying to pass a parameter using http://localhost:8080/RestWebApp/resources/greeting?name=Krt_Malta but the PUT method is not being called... Is this the correct way to pass a parameter or am I missing something?
I'm a newbie to Rest bdw, so sry if it's a simple question.
Thanks! :)
Krt_Malta
The second URL is a plain GET request. To pass data to a PUT request you have to pass it using a form. The URL is reserved for GET as far as I know.
If you build the HTTP-header yourself, you must use POST instead of GET:
GET /RestWebApp/resources/greeting?name=Krt_Malta HTTP/1.0
versus
POST /RestWebApp/resources/greeting?name=Krt_Malta HTTP/1.0
If you use a HTML-form, you must set the method-attribute to "PUT":
<form action="/RestWebApp/resources/greeting" method="PUT">
For JAX-RS to mactch a method annotated with #PUT, you need to submit a PUT request. Normal browsers don't do this but cURL or a HTTP client library can be used.
To map a query parameter to a method argument, JAX-RS provides the #QueryParam annotation.
public void putWithQueryParam(#QueryParam("name") String name) {
// do something
}
You can set:
#PUT
#path{/putHtm}
#Consumes("text/html")
public void putHtml(String name) {
this.name = name;
}
and if you use something like google`s Volley library you can do.
GsonRequest<String> asdf = new GsonRequest<String>(ConnectionProperties.happyhourURL + "/putHtm", String.class, yourString!!, true,
new Response.Listener<Chain>() {
#Override
public void onResponse(Chain response) {
}
}, new CustomErrorListener(this));
MyApplication.getInstance().addToRequestQueue(asdf);
and GsonRequest will look like:
public GsonRequest(String url, Class<T> _clazz, T object, boolean needLogin, Listener<T> successListener, Response.ErrorListener errorlistener) {
super(Method.PUT, url, errorlistener);
_headers = new HashMap<String, String>();
this._clazz = _clazz;
this.successListener = successListener;
this.needsLogin = needLogin;
_object = object;
setTimeout();
}