What do I need to create a RESTful API Server in Java? - java

I would like to build my own RESTful API Server and I have no idea what I need for that.
I'll tell you a bit about the project:
On a Webservice (www.mysite.com/) users can register and manage their account and so on. But they also can use the RESTful API (mysite.com/api/...) and can do there pretty much the same via REST.
What is a good way to realize that? Do I need to use jetty or something similar?
Should I split web service and restful api ? what I a good architecture for that?
Thanks :)

You can use Spring controller for building a restful server. You can run it on tomcat or jetty doesn't matter.
Check this url : http://static.springsource.org/spring/docs/3.0.0.M3/spring-framework-reference/html/ch18s02.html

Tomcat and Jersey are easy to get up and running. I've had some issues with Tomcat 7 and Jersey, but with Tomcat 6 it was straight forward.
This tutorial is quite easy to follow. It's a bit old, but the principle remains the same.

IBM provides good set of information and tutorials about building RESTful web service with Java (Link). After getting your web service running, you can deploy it to Amazon. Take a look at AWS Elastic Beanstalk.

In 2017 one of the best solutions would be to use spring boot. Gives you great effects without writing tons of code.
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}

I found a simple example at http://coder2design.com/rest-web-services/
to build a REST application.
XML Schema(xsd) is used to build domain classes.
Eclipse EE is used as IDE and Maven for building.
Jersey as a framework for REST
Hibernate for persistence layer.
MySQL as DB
All other configurations are nicely explained.

Related

Independent Rest API service and Website

I have designed a independent Angular 4 application in Node.js and also design a rest api service using dropwizard. I have tried to connect website as static asset for dropwizard service but I could not integrate it properly. I had gone through several links on google, everywhere repeating same thing which I have tried many a times.
One thing I thought of deploying both of these independently but a question rises how?? How can I deploy two thing on server under same domain?
Another thing which can be possible is Integrate Dropwizard with angular app, which I have tried and read many docs. but still If someone can give some sort of link related to creating these in maven is most welcome.
I need some insight regarding this.
Thanks.
How can I deploy two thing on server under same domain?
Deploy them behind a reverse proxy like nginx and use different context paths.
You could serve the result of package Angular app as a Dropwizard asset
Then you can call the rest API from Angular.
Remember that Dropwizard is just some code to glue together Jersey (for rest), Jetty (webserver) and Jackson (JSON), with the main purpose of simplifying configuration.

how to build a spring boot jar exposing both rest and soap service

I have been contemplating on building a test jar for a community of developers in order to expose a preview of a next release of an API (having stubs returning expected response with exact format etc). We do have both REST and SOAP API. I guess it won't be any problem building the REST Service as the net is flooded with example. It was quite surprising there isn't much of concrete example of how to build SOAP service (JAXWS) with spring boot with embedded jetty.
What I expect to achieve is one single jar with both APIs. I am rather comfortable developing a java first services. I have seen a post in stackoverflow but it doesn't clearly outline steps to achieve that. I know it's possible because dropwizard guys have similar project.
I will be grateful if there is any resource with example on how to handle SOAP web services in spring boot.
Thanks you in advance
Spring already supports JAXWS through *JaxWsServiceExporter and SpringBeanAutowiringSupport (in spring-web). The *Exporter approach doesn't quite mesh with the REST stuff because it isn't in the embedded container. You'd end up with an app listening on 2 ports (one for XML and one for JSON). If either of those works then you have a solution. If you don't really care that much about SOAP and just want XML representations, you can use normal content negotiation features (e.g. #ResponseBody or #RestController for everything).

REST ful java web service on heroku?

how to write java REST Web service and Deploy it on heroku? Till now I just worked on simple java application on heroku, first time I am trying to deploy a web service.
Spring MVC's REST support is one option that is pretty easy to use. Or you can use Play Framework's REST support. Or there are many other options. They should virtually all run on Heroku so try a few and see what works for you.
Here's an example of a JAX-RS REST service, complete with Procfile and README for deploying to Heroku (not much to do).
https://github.com/jesperfj/jax-rs-upload-file
It uses Grizzly as embedded server and is super lightweight. Let me know what path you decide to take. Curious about what works best for you.

Integrate Groovy in an existent Web app

I have an existent web application, I'd like to add Groovy to this web app so that I can develop web service providers easily if possible.
Is there a way to write a service class (like Grails service classes) and expose this class as ws without much pain ?
Grails uses the same thing : after adding one line to the service class, that class will be exposed automatically as a web service.
Regards
GroovyWS is a SOAP based implementation of Apache CXF.
After some quick searching I found two tutorials that may be of help.
One using Jersey and the other using Restlet. The Jersey one seems simpler.
Creating RESTful services with Jersey and Groovy
Building RESTful Web Apps with Groovy and Restlet, Part 1: Up and Running

Spring Webservices : What should be good starting point?

I am totally new to Spring Web Services and so what concept should I start concentrating on and where should I be looking for them and in general what steps would you recommend to get to speed with Spring Webservices Module.
Note: I have an requirement to build Web Service for and consume Web Service from different application and I have never worked with Web Service in the past, I am looking at Spring WS option because both application are developed using Spring Framework, is this a good assumption to look for Spring WS or not ?
Any guidance and suggestion for discussion kind of approach would be highly appreciated.
Thanks.
(...) I am looking at Spring WS option because both application are developed using Spring Framework, is this a good assumption to look for Spring WS or not?
It's not a wrong assumption (bad integration between Spring WS and Spring would be a total irony) but you should not exclude other stacks on the fact your applications are using Spring. JAX-WS stacks (like Apache CFX or JAX-WS RI) provide Spring integration as well.
Personally, I like JAX-WS (that I use for contract-first web services) and, while it's hard to be more specific without more details about your requirements, I simply don't think that Spring WS offers any advantages over JAX-WS and I would probably go for Apache CXF in your case.
Maybe have a look at what others are saying in this previous SO question (please read all answers, the accepted one is not really good in my opinion).
What are your protocol requirements? Do you have to use SOAP, or are you free to use your own XML marshalling over HTTP (e.g. a RESTful approach)?
If you must use SOAP, then see this guide I wrote to Spring WS web services. If you're free to use your own lightweight RESTful web services, then see this example I wrote on RESTful web services.
I wouldn't use Spring WS ONLY because of the reasoning you provide. You need to identify more functional requirments like:
Can you use markup (JSON, XML, etc.)
Should you provide content negotiation
Do you need to provide complex objects (i.e. SOAP as james suggests)
Are you providing a RESTful service
etc.
I've worked with web services a lot in the past few years and there seems to be a few major projects for creating them:
Apache Axis (primarily SOAP)
Apache CXF (primarily SOAP)
Jersey (REST)
Restlet (REST)
There are other offshoots like Spring WS, or even Spring MVC, but you need to evalute which will work best.
Personally I use Jersey a lot, which also provides Spring integration. Jersey also has an awesome HTTP client for consuming services, but don't confuse creating a web service as being akin to consuming a web service. They are separate workflows and you could use separate third-party projects for both (e.g. Apache HTTP Client for consuming, and Jersey for producing).
Spring WS might work best for you, but my advice would be don't use it just because the other applications use it...use whatever works best and fulfills your requirements.

Categories

Resources