Using REST how to bind a value as a parameter - java

I want to implement REST calls in a web application. I took a look at the different available frameworks to achieve that. It seems JBoss Resteasy provides what I need:
#GET
#Path("book/{id}/comments")
public Collection<Comment> getComments(#PathParam("id") String bookId);
What I would like would be something like:
#GET
#Path("book/{id}/comments")
public Collection<Comment> getComments(#PathParam("id") **Book** bookId);
So instead of receiving a String I would be interested in binding directly the value. Meaning if my Book extends a AbstractEntity class, it would directly do the findById in the database.
I used to achieve this with Spring MVC by using Custom Conversion Services that would do the findById directly. Is there such functionality in RestEasy, or any other REST framework?
Thanks!

I've not done this, but RESTEasy has a StringConverter interface that might accomplish this. Check out Chapter 24 in the RESTEasy documentation: http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html/StringConverter.html

Related

Quarkus Response type canĀ“t receive a parameter

Is there any way in Quarkus where I can define the Response object with an actual object? I can only manage to use it like this at the moment:
Response findAll();
Basically what I want to achieve is something similar with Spring ResponseEntity:
ResponseEntity<List<SampleResponse>> findAll();
If you use RESTEasy Reactive, you can org.jboss.resteasy.reactive.RestResponse which has an API almost identical to javax.ws.rs.core.Response.
See the documentation for more details.

Spring boot able to accept Enum as Request parameter

So, I got into this new Spring Boot project which was already under developement and while writing API's I used Enum for #RequestParam in my controller and it worked.
I did not write any converters for this.
Later on I noticed that in this project the other developers had written custom Converter's for this.
So I decided to search the web regarding this and all solutions that came up for using Enum with Controller in Spring Boot used converter, could not find any examples without converter like how I did.
Below is one an example of how I wrote this, LoanStatus is an Enum:
#RequestMapping(value = "/loans", method = RequestMethod.GET)
public ResponseEntity<?> getPatientsLoan(HttpServletRequest request,
#RequestParam(value = "loanStatus", required = false) LoanStatus loanStatus) {}
So is this a relatively new feature that Spring Boot accepts Enums now without the need for converter's and that is why all the examples used converters or will I face some issue in feature cause I did not user converter's even though it is currently working for me?
Spring has supported String to Enum conversion since Spring 3.0. There is a ConverterFactory which dynamically creates a converter for the specific enum.
Prior to that you would need to write a custom Converter or PropertyEditor to convert enums. But basicallly with the current versions you don't need to if the String matches the Enum name.
If you want custom enum conversion (by some internal value or whatever) you still would need a custom converter.

How to return a subset of object properties from a Spring Boot restful GET call?

Newbie question...
I'm building my first Spring Boot restful service and want to support a GET call that returns a collection of entities. like:
/api/customers/
However, for certain consumers -like a list page in a web UI - they would only need a subset of the customer entity properties.
I'm thinking that I could add request parameters to my GET call to set the consumers specific field requirements, like
/api/customers/?fields=id,name,address
But what's the best way of implementing this inside the Java restful controller?
Currently in my rest controller the 'GET' is request mapped to a Java method, like
#RequestMapping(value="/", method= RequestMethod.GET)
public Customer[] getList() {
Customer[] anArray = new Customer[];
....
return anArray;
}
Is it possible to somehow intervene with the default Java to Json response body translation so that only the required properties are included?
TIA
Adding fields parameter is a good idea, best practice according to http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#limiting-fields
How to leave fields out?
1) Set them to null, possibly in a dedicated output class annotated with #JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
Or
2) Use SimpleBeanPropertyFilter See a good step by step tutorial here 5. Ignore Fields Using Filters

How to expose services written in Java thru REST as well as locally?

I want to create a new system that will be built completely using services. I want to expose these services thru REST for client applications. But for performance reasons, I also want to make sure that other services can call a given service using local calls without paying the penalty of a remote call. Is there a framework that can help me do that.
Well, the way we have implemented this is by using something like Spring MVC where the controller just calls out to a Service class - our notion of Model. The Controller thus acts as "exposing the services" as RESTful services. The rest of the codebase accesses these Services just like any other object. Since we use spring, we leverage the IOC massively.
For example, we would have something like:
public class BillingService {
public void doSomething(String someParam) {}
}
public class BillingController {
#Autowired private BillingService billingService;
public void doSomething(#RequestParam String someParam) {
billingService.doSomething(someParam);
}
}
In the above examples, the annotations are all from Spring, but you get the picture. Any other class which wants to access the BillingService method, they can do so just by accessing that class's method.
I am not sure of any framework which is targeted at exactly this problem, but my guess is, you do not need one.

Java WebService

What is the Java equivalent to Script service (like web service but with JSON instead of XML) in the .net world?
I am looking for a way of creating and invoiking web services using java. I prefer to find a way that will allow me define a method that will act as web service. I don't like the solutions of "dedicated jsp or servlet" for the specific request.
Is there any wat of doing so?
There are a lot of frameworks that help you to do this. I personally prefer Spring.
But you can just search for "Java based RESTful web services frameworks". Here is a list of such framework from Wikipedia: http://en.wikipedia.org/wiki/List_of_web_service_frameworks
Enjoy.
You can use libraries like Jersey or RESTeasy for the implementation of web services. For the consumers, you can use the built in classes of the same libraries or, if you prefer, can use Apache HttpClient
I personally prefer using Jersey + HttpClient combination :)
I would prefer RESTful services which fits for your need of "I prefer to find a way that will allow me define a method that will act as web service." Just with REST annotations You can set a method as a Service.
Code Snippet simple REST
#Path("/rest")
public Class MyFirstService
{
//Method without Path parameters
#GET
#Path("/name")
#Produces("application/json")
public String getMyName()
{
return "My Name:";
}
//Method with Path parameters
#GET
#Path("/name/{id}")
#Produces("application/json")
public String getMyName(#Pathparam("id")String Id)
{
if(Id.equals("1")
return "My Name:";
else
return "None";
}
}
RESTful Services give four major Services as -
GET
PUT
POST
DELETE
I would recommend JEE6, especially if your focus is going to be on REST based services. Glassfish 3 and JBoss 7 are neck and neck now with their implementation, either would be a good choice (though JBoss is my personal preference). With the JAX-RS and JAS-WS specifications you simply annotate your classes and they become web service capable:
Make your bean a SOAP based web service:
#WebService
public class AccountImpl {
// You can explicitly mark the methods you want to make available in your WSDL
#WebMethod
public BigDecimal getBalance() {}
}
Make your bean a REST based web service:
#Path("/rs/account")
public class AccountImpl {
#GET
#Path("/balance")
#Produces("application/json")
public BigDecimal getBalance() {}
}
The code snippet is just an example. You can find more resources online for learning JAX-RS and JAX-WS. Here are a few links:
RESTEasy
Jersey
JAX-WS Tutorial
Note: I included information about JAX-WS for reference only. You indicated you wanted to build JSON services, which implies REST

Categories

Resources