What is a good technology stack for a Java based web application with REST support?

Evening everyone :)

I am looking to create a Java web application. I assume that it will use Spring web MVC and JSP, however I would like to show certain functions as REST calls so that I can create an android client.

Does Spring offer anything to help me in this area? How can I save the REST code and the web interface code separately, but should not essentially support 2 versions of my application (one for the Internet, one for REST clients).

Not looking for spoon food, just some pointers to where I should start reading.

+4
source share
7 answers

As already mentioned, Spring has some nice built-in REST support. Combined with annotations, this allows a truly simple RESTful API setup. Spring can be customized with various prominent transformers , which can automatically respond with a different data representation depending on the Accept header, for example. That way you can automatically return JSON or JSP from the same data, see ContentNegotiationViewResolver . After that, your controller and model can be ordinary and deployed once, leaving work at the presentation level.

I used this approach before to return JSON when the request was through AJAX and a JSP representation built with the same data when accessing the browser.

+5
source

Jersey is a pretty elegant tool. It integrates well with tools like Spring, Guice, and Jackson to provide you with a fairly easy way to create RESTful resources.

+4
source

The jersey is pretty simple, works well and serves as a reference implementation for loading. In addition, it has good REST client support, most of which is likely to fall within the JAX-RS specification.

Regarding marriage using Spring MVC, I would recommend that you customize your application model so that you have facades (service classes) that provide all the functionality you need and then simply reference them as needed in your MVC code or REST code. You should not duplicate business logic

+4
source

We are using Restlet right now, but RESTEasy also looks promising. Both options allow returns based on the Accept: header in the request.

+2
source

You can do this using Spring 3.0. Spring 3.0 @PathVariable ability to specify @PathVariable to pull values ​​from a URL path (previously this was not easy in Spring MVC).

You can also use @RequestMapping to specify the HTTP method (s) that should respond to each method in your controller.

I also use Spring Security to implement the functional key of the API key. This way you can restrict access to your API in a way that REST clients can easily implement. I had to extend org.springframework.web.filter.GenericFilterBean and add an appropriate authentication like this

 SecurityContextHolder.getContext().setAuthentication(apiKeyAuth) 
+2
source

Apache CXF integrates well with Spring and offers many methods for providing services. In the overview section of the CXF homepage:

CXF helps you build and develop services using front-end programming APIs such as JAX-WS and JAX-RS. These services can speak different protocols, such as SOAP, XML / HTTP, RESTful HTTP, or CORBA, and work on various vehicles, such as HTTP, JMS, or JBI.

+1
source

Source: https://habr.com/ru/post/1342535/


All Articles