Can I add REST to my existing BlazeDS spring webservice? - java

I have existing blazeDS web-services which need to be preserved as is for various legacy reasons.
I now have the need to expose the same functional services via a rest api and marshall the previous binary VOs via json.
I want to know if I can somehow use both #RemotingDestination and #RequestMapping at the same time on the same class? Have it cater to both request types?
Thanks

The easiest way to expose the same functionality to both REST and Blaze is to create a wrapper methods for the REST endpoint and have it proxy through to the original Blaze exposed method.
Simple Example assuming a simple GET:
#Service("userService")
#RemotingDestination(channels={"my-amf","my-secure-amf"})
public class UserService {
#RemotingExclude
#RequestMapping("/user/{id}", method=RequestMethod.GET)
public String getUserByIdRest(#PathVariable String id) {
return this.getUserById(id);
}
#RemotingInclude
public String getUserById(String id) {
//..
return id;
}
}

Related

How to create a java client for a Spring service?

I have a service definition using Spring annotations. Example (source):
#RequestMapping(value = "/ex/foos/{id}", method = GET)
#ResponseBody
public String getFoosBySimplePathWithPathVariable(
#PathVariable("id") long id) {
return "Get a specific Foo with id=" + id;
}
The question is whether spring (or another library) can auto-create a remote implementation (client) of the same API without the need to manually type paths, method type, param names, etc. (like needed when using RestTemplate)?
Example of an such a client usage:
FooClient fooClient = new FooClient("http://localhost:8080");
String foo = fooClient.getFoosBySimplePathWithPathVariable(3l);
How can I get to such a client "generated" implementation"?
You are probably looking for Feign Client. It does everything you need: calling one service via HTTP is similar to calling method of Java interface. But to make it work you need Spring Cloud, standard Spring framework doesn't have this feature yet.
You can generate it using Swagger Editor. You shoud just define the path of the resources and then it'll generate for you the client for almost any language of your choice

RESTEasy - Best practices to override service method to implement some generic logic across the different methods (like in servlet)

Ex: I have some common logic across different resources
#Path("/rest")
public class AddUser {
#GET
#Path("/AddUser/{ext}/{userId}")
#Produces(MediaType.APPLICATION_JSON)
public String addUser(#PathParam("tenantId") String tenantId, #PathParam("userId") Integer userId) {
//I have some common logic here
}
#Path("/newrest")
public class AddUser1 {
#GET
#Path("/AddUser/{ext}/{userId}")
#Produces(MediaType.APPLICATION_JSON)
public String addDifferentUser(#PathParam("tenantId") String tenantId, #PathParam("userId") Integer userId) {
//I have same common logic here as well
}
}
Which class i can extend to overwrite common logic for bot the rest services?
Overriding Service method is not recommended Should I override service ?
Take a look # ContainerRequestFilter An extension interface implemented by container request filters. You can handle your common logic here.
You should probably not have bussiness logic directly in the communication layer. Imagine adding soap interface using different technology it will be imposible to use any common entry point.
You'd rather write service class and call it wherever you need.

Calling Spring Rest service from c++ or Ruby

I am learning Spring Rest services, I have question regarding Spring rest services
Is it possible to call Spring Rest service from other language like c++ or Ruby, where c++ or Ruby will act as Client and Spring Rest service as service or resource provider.
If it's possible can some one explain simple, detailed manner with example.
The reason of asking the question, if we develop a web service using Jax-ws, the interoperability will happen across the technologies like calling Java based web service calling from C++ or and vice versa, can same thing happen using Rest service which is developed in Spring Rest or using Jersey api framework.
A REST call is just an http call. The service doesn't care what language the client is coded in - could be a browser, a mobile phone, written in c++, java, c, objective-c, it doesn't matter.
Typically if you have object data to pass from the client to the service, you would encode it in JSON or XML.
Yes, it is possible. The key of course is Serializing/Deserializing Data. As long as your Rest service accepts serialized data as input, and returns serialized data as output.
For Example, lets say you have an endpoint http://www.example.com/public-api/foo, with acceptable method GET (it provides data).
In Spring, you have a resource named Foo.java, that takes the following form
class Foo implements Serializable {
private static long serialVersionUID = -1L;
private String someProperty;
public Foo() {
...
}
public String getSomeProperty() {
return this.someProperty;
}
public void setSomeProperty(String someProperty) {
this.someProperty = someProperty;
}
}
With the following Controller
#Controller
#RequestMapping(value={"/"})
class FooController {
#RequestMapping(value={"/foo"}, method={RequestMethod.GET})
public HttpEntity<Foo> foo() {
...
Foo foo = new Foo();
...
return new ResponseEntity<ResourceSupport>(foo, HttpStatus.OK);
}
}
When you access this in your browser, it will return the following text
{
"_self": "http://www.example.com/public-api/foo",
"someProperty": ...
}
This output (in JSON), can be parsed in Ruby and C++ (or any language really) quite simply.
Inputting is the same way. Instead of parsing JSON, you would just POST or PUT JSON data that conforms to whatever resource you're trying to input. To POST or PUT a new Foo object, you just POST or PUT JSON data with the appropriate Properties.

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