Using Maven & Jax-RS, where do I put my files? - maven

I created a maven project using a Simple Web App Archetype. Then I went in and created a Jax-RS module. Now I have 2 index.jsp. Should I have created my project without an archetype and then added a Jax-RS module?
I also plan to add some persistence to the project. Maybe hibernate.

You don't need index.jsp for JAX-RS. Every JAX-RS servlet (Jersey, RESTeasy, etc.) provides its own Servlet, that dispatches REST requests into your #Path annotated resources. You configure the servlet in WEB-INF/web.xml.
The rest looks correct in your project layout.

Related

Simple REST service with Spring framework not using Spring Boot

I want to start with the most simple Maven Project with a simple REST resource not using Spring Boot and generate a .war artifact that is deployed in a servlet container. I am using Eclipse IDE. So I would like to know what is the basic things in place needed to create such a Project.
I think I need at least this dependences:
Spring-core, Spring-mvc, Spring-web, Spring-context,
I also need the stuff with a class annotated with the #RestController annotation, with some method annotated with the #Requestmapping and so.
But whats the minimum content I should have in the WebContent directory and its subfolders META-INF, WEB-INF ... in order to the servlet container to know how to use the .war component? I dont want any HTML nor JSP pages.
In your WEB-INF folder, you will need a web.xml file. This is where you will configure your dispatcher servlet. This is the part of your application that receives requests and delegates them to the appropriate part of your application.
You will also need some sort of REST configuration file. You can define beans for Spring and component scan config.
A good explanation of this can be found here, https://www.programming-free.com/2014/01/spring-mvc-40-restful-web-services.html

Force Spring Boot to use servlet mapping in web.xml

I'm currently trying to shift my existing dynamic web project to Spring boot project and it uses web.xml for servlet mapping. I understand that spring would ignore the web.xml file, what should be the correct approach for spring to use the existing web.xml? And yes, I still need to stick to using web.xml for this project.
I'm kinda new to this, please guide me through! Thanks!
I suppose that you need to stick with a web.xml because your container uses an older version of Servlet than 3.0.
Spring Boot is built on Servlet 3.0. You have to update your main class to extend SpringBootServletInitializer and override configure method, which tells spring to use its Servlet 3.0 support. Embedded containers like Tomcat need Servlet 3.0, so if you want to start your project during the development process (including JUnit tests) in embedded containers, I think, from what I know, the only way is to rewrite your web.xml to Servlet 3.0 java config. But if you really want to deploy you app in an older container, you still can by using spring-boot-legacy module. It allows you to use web.xml for older containers; only thing you have to do is to add
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener in your web.xml.
For more information about deploying war in an old container, take a look at Spring Boot's official documentation.
Spring Boot uses Servlet 3.0 APIs to initialize the ServletContext
(register Servlets etc.) so you can’t use the same application out of
the box in a Servlet 2.5 container. It is however possible to run a
Spring Boot application on an older container with some special tools.
If you include org.springframework.boot:spring-boot-legacy as a
dependency (maintained separately to the core of Spring Boot and
currently available at 1.0.2.RELEASE), all you should need to do is
create a web.xml and declare a context listener to create the
application context and your filters and servlets.
This is a very old question, but I'm currently on the same situation and I will share my experience.
I extended the SpringBootServletInitializer class to create my #SpringBootApplication, and it will INCLUDE your web.xml configuration by default. I'm still able to declare or edit existing servlet mapping, and it will be taken in account.

Can Jersey resources classes be instantiated in Spring Boot if they're external?

I'm developing a Spring Boot app using Jersey for Rest.
The app will regularly have Rest controller and supporting classes added to it, basically adding small, specific logic at specific endpoints.
I'd like to be able to drop jars containing Jersey controllers into a directory specified by loader.path and have them created (including Spring auto-wiring) by just restarting the server.
Some Jersey resources form the base of the application and these are working fine because I've registered them via a ResourceConfig class but I'd really like to add resources without requiring a rebuild of the app.
Is this possible? I'm failing to get the resources created, I'm not even sure they're in the classpath.
In application.properites, I added:
-Dloader.path=lib/,resources/
and I've copied the jars into these folders, but they don't get instantiated.
Anybody got any ideas?

Initialize Spring Application Context for spring projects packaged as a jar

We have several java projects. Most of them are built with Struts 2.0 framework and few built with Spring 3.2. We want to consolidate all the back-end integration service into a separate project using spring 3.2 and import this jar file on all the projects. Here are my questions
What is the best way to initialize spring application-context for a jar based spring project? This jar is utilized by multiple web-project that are built using Struts and other non spring MVC frameworks.
I read How to package spring based library for reuse?. However, this question didn't answer on how to auto-load the application context when you a call a Service from the built spring-example.jar file.
For example. I have a WeatherService.java class in spring-framework.jar file. I want to import the spring-framework.jar file into another Struts-MVC based application and call WeatherService.java from an Action Class. I want the spring bean configuration to initiate automatically when calling the WeatherService.
You can use #Import annotation if using Java configurations or <import> tag if using XMl configs. With this approach you can reuse import one Spring context into another one.
Link to documentation:
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-java-using-import
EDIT:
If you are using maven, place your application-context.xml into src/main/resources. If not, make sure that it's on classpath.
Than if you are using XML config do
<import resource="classpath:application-config.xml"/>
or if you are using Java config do
#ImportResource("classpath:application-config.xml")

How to setup a Spring multimodule project?

I'm new to Spring and try to setup a project which is split into 3 submodules. As build tool I'm using maven. My problem is, that I don't know where to add Springs "magic".
My 3 submodules are "ORM" (holds all the hibernate staff to access the database) "BusinessLogic" (which should hold the complete logic) and "WebApp" (adds as the only "client" to the app / logic).
I want to use SpringMVC for the WebApp which seems to be no problem. As "BusinessLogic" should hold the complete logic I thought of adding the Spring related stuff (Bean definition / DI) in that module. But then I don't know how to setup Spring when accessing the module form the webapp.
The hole project is being ported from a JavaEE / JBoss app where "ORM" and "BusinessLogic" (implemented as EJBs) where put into one .ear archive and the webapp into a seperate one (.war). JNDI was used to access the beans from the webapp, but I completely want to decouple the application from JBoss and deploy it on a Tomcat webserver.
At the moment I've created all three modules as separate Maven projects ("ORM" and "BusinessLogic" as .jar, "WebApp" as .war packaging), linked by a "parent" project.
Thanks for any hints on project setup :).
Greetings
Ben
you could configure spring context in your web.xml and you can perform import of Spring sub-modules context. You can add import's configuration of sub-modules in your webApp application context.

Resources