Adding Spring boot to existing JSP project - java

We have an existing JSP project that serves a website and a REST interface. However its an old project (started in 2013) and now it does not scale well with new requirements. We would like to add Spring REST interface under the same project (sharing same tomcat and sessions) until we we migrate completely and drop the old REST interface (and the website) entirely.
Current project structure is:
Root/
website/
rest/
WEB-INF/
web.xml
classes/
META-INF/
context.xml
Current rest interface is accessible via /rest/xyz.jsp
Now we would like to use Spring and migrate rest/* such that they will be accessible via /api/*
How do I integrate Spring boot into this project? what set of configuration do I need to make?
Spring boot docs and internet weren't helpful.

Related

Using Cesium.js inside Spring MVC project

I have a project which is built on top of Spring MVC (not Spring boot). I used the Spring Tool Suit(STS). I want to add the functionalities of Cesium.js project to my Spring Project but the problem is Cesium is a Node.JS application.
I do not know how should I reference the Cesium project inside my Spring MVC project.
I read the documentation but it was not clear what to do.Basically it says you have to move the root folder to your project and then use it but with Spring MVC its a little bit tricky.
I moved the resource folders to a folder under my resources in Spring MVC project and kept the index of cesium in my views folder, made a route to it in my controller then I changed the links in the index according to spring links and also changed the suffix to jsp instead of html and it all worked out.

Is it possible to nest a Spring Boot application inside another Spring Boot application?

I have a Spring Boot RESTful microservice that a customer would like to nest inside their Spring Boot application.
Could someone tell me whether this is possible?
I was hoping this would be as simple as adding a dependency on my application in the customers maven pom file and then excluding the tomcat dependency since the customer already uses the embedded tomcat.
Thanks,
Ben
Since they already use Spring Boot to start their app, you can simply mark all Spring Boot dependencies as provided in your Maven POM, this would exclude it from the JAR as well as embedded Tomcat and all related dependencies. Also make sure you don't build your JAR as a Spring Boot executable (should be the default if you're not using the spring-boot-maven-plugin).
On the customer side, they would need to include your JAR as a dependency, and possibly add a scanBasePackages property to their #SpringBootApplication, to auto-discover your application classes, if they don't reside in a package under the one that #springBootApplication is on. Also, they'll need to be mindful of any URI collisions between your app and theirs, as the two will be sharing the same environment.

Can a non spring web application use a jar which is built using spring + hibernate

We are building a java application using spring hibernate maven. The jar will be used in a non spring web application that uses ejb and plain jdbc. Ejb will access the jar interface methods which in turn makes hibernate calls.
What changes are needed in the web application to load the spring context during start up? Is it feasible?
If you want to keep your jar decoupled from web application you should do this:
Copy your jar and all dependant jars to webapp WEB-INF\lib directory (or pom if maven)
Your Spring Context should be loaded in a static variable, in a safe thread manner; or if you want to integrate it to the webapp lifecycle you can do it in several ways: web.xml, web-fragment or using a servlet initializer. There are lot of documentation in Internet
If you continue to use hibernate or change to JDBC, the DataSource should be provided generally by the web container, so you must use InitialContext.lookup("") to get the actual DataSource.

How to combine Yeoman scaffolding with existing Java directory structure

In my existing web project the directory structure for the served html content while development with jetty is "myProject/src/main/webapp/"
Now, I want to integrate an angularjs project here.
I've played a little bit with Yeoman.
If I'm scaffolding with yeoman, I'm wondering how I can integrate it into our existing dev and deployment structure.
I suppose to use the main folder "myProject" to run yeoman scaffolding would be fine. Then I would get a "myProject/app/" diretory for all my frontend stuff. Should I instruct somehow (how?) my jetty server to use ".../src/main/webapp/" as an alias for the new app directory?
We use jetty mainly as a proxy for requesting the backend. Is there also a way to do a live reload similar to "yeoman server" in combination with jetty?
Take a look at my answer on how to do Django-Yeoman integration.
Architectural concepts will be the same, even external articles (definitely must-reads) are Java-based.
In short:
Use yeoman-maven-plugin. If you are on Gradle that's still ok. Even better, since you will have better control over which grunt tasks are being invoked.
Your project structure should resemble this:
pom.xml
src/
main/
java/
...
resources/
...
webapp/
WEB-INF/
yo/
dist/
<<the rest of the Yeoman-generated stuff>>
Yeoman generators, including the one initialising the frontend part, should be invoked exclusively from yo directory.
The plugin takes care for copying production-ready yo/dist to WEB-INF.
All you have to do is to serve the latter as a static resource.
Config for Spring MVC (dispatcher servlet):
<!--Yeoman static content-->
<mvc:resources location="WEB-INF/yo/" mapping="/**"/>
One should aim for similar config when using other technologies, like Jetty, or pure Servlet config.
The rest, particularly dev setup, is described in referenced answer.

Is it possible to share the same MVC between external Jars and a War in Servlet 3.0 environment?

I understand that Servlet 3.0's enhancements have made it possible to display a .jsp from a .jar, based on Can I serve JSPs from inside a JAR in lib, or is there a workaround?
However, I don't seem to be able to connect my View (jsp in jar WEB-INF/lib Tomcat 7 and classic spring MVC context configuration in a War) with the Model and the Controller of my Web App.
Is there a good way to share the dispatcher Servlet, or perhaps create a CustomViewResolver which could scan .jsps included in external JARs, and actually plug my jar into a unique spring context?
With JSP you have the problem of compilation.
So you at least need to precompile them, to have them included. Then it should be possible, since after compilation a JSP is basically a Servlet.
If you would use another view technology like Velocity, Freemarker or JSF based on Facelets, you can very easily use a classpath based ViewResolver.

Categories