I have a JavaEE MVC web app running on Wildfly 10. Now there is an idea to separate back-end and front-end. I want to create a RESTful Web Service with Spring Boot so it will mediate between front-end (Angular) and back-end (JavaEE app). For example the front-end will make AJAX calls to my RESTful Web Service and will handle the response returned in JSON format.
Within my JavaEE app I could make REST calls this way and the appropriate method would be invoked depending on the path. For example:
#Path("products")
public class ProductsResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public List<Products> getAllProducts() {
// return list of products
}
}
But for now I do not understand how can I make the Spring Boot RESTful service get data from my current JavaEE web app? They will be running on different servers.
UPD: providing more info on my JavaEE app, it has a DAO layer and EJBs as a service layer. For example:
public class ProdBean implements ProdBeanInterface {
#Override
public List<Product> getAllProducts(DataSource ds) {
ProdDao productDao = new PostgreSqlProdDao(ds);
return prodDao.getAllProducts();
}
The ProdBeanInterface is injected into a servlet controller and is further used for DB access fetching data from DAO layer.
Related
I have written an interface and an implementation class - which exposes a few REST endpoints (eg. /mock/abc/user) and returns some static values as json response. How do I bundle these code into an ear/war and deploy them in a lightweight standalone server such as Wildfly.
Interface
#Path("/mock/server")
public interface MockServer{
#Path("/user")
#GET
#Produces({MimeTypes.APPLICATION_JSON})
Response getUserDetails();
}
Impl Class
public class MockServerImpl implements MockServer{
#Override
public Response getUserDetails() {
...
}
I am supposed to deploy it in a separate server and hit these endpoints from my JUnit tests.
Set Context path differently for both Webservice and Rest
I have an application which contains both implementation of Webservices and Rest services and I am looking for an solution to set context path for both Webservices and Rest services differently using yml/properties file
How to configure servlet dispatcher to work properly?
I would like to have:
localhost:8080/ws/* - webservice
localhost:8080/web/* - MVC components
servlet:
context-path: "/ws"
It sets globally for both webservices and rest services , How to make it independent to each other with out programming?
Using Spring Boot (with Spring Starter Web) you could achive what are you asking for with the annotation #RequestMapping.
You could put #RequestMapping(value="/ws") on the class declaration of every rest controller and #RequestMapping(value="/web") for web controllers.
For both rest and web controller than you could use other annotations to specify method path, i.e #GetMapping(value="/methodPath").
#Controller
#RequestMapping(value="/web")
public class WebController{
#GetMapping(value="/method")
public String method(){
...
}
}
#RestController
#RequestMapping(value="/ws")
public class RestController{
#GetMapping(value="method")
public String method(){
...
}
}
I am working with Spring Boot 2 and I would like my requests to be handled asynchronously.
In Java EE, one can use Asynchronous Processing for Asynchronous Servlets as in this link. The following is a related example:
#WebServlet(urlPatterns={"/asyncservlet"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet { ... }
and the above allows to use AsyncContext.
But in Spring Boot, I have the following #RequestMapping. How do I make it handle requests in Asynchronous mode and also supporting AsyncContext? How do I leverage the use of an Asynchronous Web Servlet?
#RestController
public class myRestController {
#RequestMapping("{resource}/**")
public void resourceRequest (#PathVariable("resource") String resource) {
// example:
// ... some long running calls such as database communication
// ... convert request through AsyncContext ctx = req.startAsync();
// etc
}
}
Note that returning void is intentional.
I found the following SO answer How to register a servlet with enabled "async-supported" in Spring-Boot? saying that "Spring Boot will automatically register any Servlet beans in your application context with the servlet container. By default async supported is set to true so there's nothing for you to do beyond creating a bean for your Servlet." but I am not using any #WebServlet annotations anywhere in my program, just the #RestController annotation. So how do I ensure that I am benefitting from asyncSupported option?
I've been using Spring annotations such as #RestController and #RequestMapping to generate simple services in a Spring Boot Web application.
So I have this trivial example working correctly:
#RestController
public class HelloController {
#RequestMapping("/")
public String sayIt() {
return "Hello!";
}
}
Now, I would like to separate out an API library (jar) with only the REST interface and the DTOs. One or more separate libraries would provide the actual implementations of this interface. I can then use the (lightweight) API library on the client-side to generate REST client proxies to talk to any of the implementations.
So... are there any annotations or configuration to mark REST interfaces vs. implementations separately? If not, what is the Spring-y way to achieve this instead of using JAX-RS annotations?
#Something1
public class HelloServiceApi {
#RequestMapping("/")
public String sayIt();
}
#Something2
public class HelloServiceImpl implements HelloServiceApi {
public String sayIt() {
return "Hello!";
}
}
I would advise to have a jar that contains the DTO objects only, without any logic. It then can be used by both the REST server and the client to transfer objects.
The client should not be dependent on the REST war/jar or the logic.
Furthermore, I would try to make sure my controller doesn't hold any logic aside from, maybe, transferring the DTOs into domain model objects which will then be passed to the business logic layer.
In my opinion, the REST layer should be only responsible for the external API, argument handling, sending to a layer bellow (service layer) and preparing a response.
That being said, you should have your different implementations at the service layer. This allows having the API/REST layer untouched and constant.
The service layer (being providing in different implementations) should respect some common interface that is later injected in the above rest layer.
Have I responded to your question?
A HTML5 UI is connected to the backend (REST Jersey to business logic to Hibernate and DB).
I need to create and maintain a session for each user login until the user logs out.
Can you please guide me on what technologies/ APIs can be used.
Does something need to be handled at the REST Client end also..
Using JAX-RS for RESTful web services is fairly straightforward. Here are the basics. You usually define one or more service classes/interfaces that define your REST operations via JAX-RS annotations, like this one:
#Path("/user")
public class UserService {
// ...
}
You can have your objects automagically injected in your methods via these annotations:
// Note: you could even inject this as a method parameter
#Context private HttpServletRequest request;
#POST
#Path("/authenticate")
public String authenticate(#FormParam("username") String username,
#FormParam("password") String password) {
// Implementation of your authentication logic
if (authenticate(username, password)) {
request.getSession(true);
// Set the session attributes as you wish
}
}
HTTP Sessions are accessible from the HTTP Request object via getSession() and getSession(boolean) as usual. Other useful annotations are #RequestParam, #CookieParam or even #MatrixParam among many others.
For further info you may want to read the RESTEasy User Guide or the Jersey User Guide since both are excellent resources.