How to use two URL mappings - java

I want to migrate my application to Spring Boot Jar Deployment. It currently uses Spring 4 without Boot.
I have a REST-API listening at /api/* and a Javascript-Frontend at src/main/webapp which can be accessed at /*.
Now i don't find a way doing the same in Boot.
I managed to get my api listening at /api/* by changing the server.context-path property but I didn't manage to register a second servlet to serve my js-frontend at /*. I know that src/main/webappis not supported by a jar deployment and i also know that Spring Boot serves static files from src/resources/public and src/resources/static. But in my case these folder then also points to /api/* because of the server.context-path change.
I tried is to register another servlet as a bean. This destroyed my api-endpoint.
What would be the best way to achieve this?

First option - copy everything from src/main/webapp into src/resources/static. This is where Spring Boot looks for that static content.
Second option, keep using src/main/webapp, but configure your build to copy the static resources into target/classes/static. I provided the Maven config for this on a previous answer: Refreshing static content with Spring MVC and Boot
Note that if you go with the first option, if you wish to modify content and have it automatically reloaded without running a build, you will need to run your application inside your IDE. Going with the second option gives you the usual Tomcat reloading of static content if you're executing your jar, but could lead to some confusion. Personally, I go with the second option most of the time, as I like to run the application on the command line and edit my HTML and JavaScript with Chrome Dev Tools.

For multiple mappings, you will have to add one per mapping and map that in controller like below.
#Controller
#RequestMapping(value = { "${url.mapping}","${url.mapping.two}" })
public class CustomerController{

Related

How to configure a Spring Boot Application that uses tomcat and has a dependency to another Application uses tomcat

I have a Spring Boot application (App1) that uses an embedded Tomcat. To specify the Port I added server.port=8080 to the application.properties in my main application
and it works fine.
Now I add a dependency to another Spring Boot application (App2) in the pom.xml (because in App1 I need access to the Beans from App2). App2 uses also an embedded Tomcat on a different port.
But when I run App1 it crashes because App2 want to also use the same server.port and configurations - that obviously cannot work.
Is this even possible what I want to achieve? And how would I do this that I can run both App2 in App1? If this is not possible, is it possible to access the Beans from App2 in App1?
I see two options, depending on which one suits your use case conceptually:
you have two Spring Boot applications that need to share the same code: extract this code to a separate project (a regular Java library), build it as a jar file and include into both projects as a dependency; an example could be found in the Creating a Multi Module Project guide;
you have two Spring Boot applications, one of them needs to access the functionality of another: provide and access this functionality as an API, instead of directly importing the beans. There are multiple ways to do this. For example, you could expose the bean functionality as a REST API using Spring's #RestController, and access them from the other side using RestTemplate. See the following guides: Building a RESTful Web Service, Consuming a RESTful Web Service

how to add a parameter to spring project before <projectname>/login and after <localhost>:<portnumber> without hardcode?

I'm working on a Spring MVC project. When I run the application the URL is:
http://localhost:8080/insureYou/login
but I want:
http://localhost:8080/contextroot/insureYou/login
Is there any way of doing it without hardcoding?
In a spring-boot project you can set the context-root by specifying the following property in the application.properties file:
server.servlet.context-path=/yourcontextroot
Without spring-boot, it depends on the webserver and Tomcat offers a number of options.
I would personally opt for a META-INF/context.xml file in your war file containing the necessary information but you can also include the information in the server.xml file or in a ROOT.xml file.
See the following links for further guidance:
How to set the context path of a web application in Tomcat 7.0
https://tomcat.apache.org/tomcat-8.0-doc/config/context.html
https://www.baeldung.com/tomcat-root-application
This type of deployment however sometimes is handled separately, through an Apache server reverse-proxy or through URL rewriting.
I recommend you ascertain whether this type of need is already taken care of by your company's deployment procedures, as you may not need to deal with it at all.

Spring Boot - Proper location for html files

I am new to Spring Boot and I am trying to add a simple html page for my project
At the moment, my project's structure is the following:
Having read the following:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content
and other answers in StackOverflow, I'd expect to see the contents of index.html when I visit http://localhost:8080/
What could I be missing?
As there is no webapp in SpringBoot project , we can place the html files in either in
src/main/resources/resources/index.html
src/main/resources/static/index.html
src/main/resources/public/index.html
Remember the above is in highest to lowest precedence.
To check your file run the main class in eclipse and go to http://localhost:8080/index.html
First of all the correct way to serve files in Spring Boot is described in
cannot find html pages with spring boot
After adding the directories required, I tried /services/login.html where "services" is the mapping for my Dispatcher servlet.
So what happens here is that Spring Boot will only answer to requests sent to /services/* even if these requests are pointing to something other than a controller.
I guess that this makes sense in the world of Spring Boot but when I tried in the same in a Spring MVC project (no Spring Boot) I was able to access simple html files that I had simply added into my WEB-INF directory

Load webapp on container startup without web.xml in Tomcat+Jersey?

I currently have a Jersey webapp without a web.xml. It deploys nicely, but doesn't start up until it receives its first web request.
To get the webapp to load at startup, I could create a web.xml for the webapp and give a load-on-startup tag. However, I'd strongly prefer not to make a web.xml.
Is there a way to get a JAX-RS application to load at startup without web.xml? I'll even accept a solution that is specific to Jersey and/or Tomcat.
EDIT: I would also accept a solution that loads all apps in a Tomcat instance eagerly.
EDIT: Let me give a little more information on how the app is being deployed, per a comment.
The deployment process is not sophisticated.
The App will live on an EC2 instance running Ubuntu 12.04. I'm setting up one instance of the App by hand; once it works, I will make an AMI of the app and create additional copies of it as needed.
To deploy the app on the initial instance, I'm simply copying the WAR file to /var/lib/tomcat7/webapps and restarting Tomcat. No other webapps will be running on this Tomcat instance.
If any additional information would be useful, let me know! I'll happily add it.
EDIT: For clarity's sake, this is how my webapp Application class looks, at a high level:
#ApplicationPath("/")
public class App extends ResourceConfig {
// ...
}
I'm using the Jersey-specific ResourceConfig class instead of the more general JAX-RS Application class because I'm using Jersey's built-in HK-2 to do some dependency injection.
The only way I can think of to do that is to switch to setting up the Jersey ServletContainer yourself and set its "load on startup" value to something greater than zero. You might use a ServletContainerInitializer (no relation--the naming is just a coincidence) to do it. If you happen to be using Spring, its WebApplicationInitializer offers the same mechanism with a slightly more convenient interface.
Another, rather hacky, way would be to write a class that extends ServletContainer and give it an appropriate Servlet 3.0 annotation, something like #WebServlet(value="/", loadOnStartup=1)
One solution would be to force a first request to the app by simply adding a call to curl or wget to your deployment script. It has the additional advantage of warming up any caches. And it can be used for testing if the deployment and the app really work. (Just check HTTP status code or some text on the response page...)

Is there a standard way to customize the deploy path in Spring Boot?

I'm exploring the possibilities of Spring Boot right now, and I'm at a slight impasse. I want to be able to run two Spring Boot applications at once, both on the same server, but at different paths (one deploys on /, the other deploys at /another-path).
Because this is an embedded Tomcat instance running within Spring Boot, there's no configuration files available for me to change.
Is there a standard way to do this? Is it possible?
Spring Boot comes with some pre-built property support. If you create an application.properties file, you can include:
server.port=<another port>
server.address=<another IP address>
server.sessionTimeout=<another timeout setting>
server.contextPath=/your-other-path
This can be in application.properties adjacent to your runnable JAR, embedded inside the JAR file, or simply applied as a -Dserver.contextPath=/your-alt-path with the java command. These are cascading, meaning you can embed one set of defaults inside the JAR, override with a local application.properties file, and then finally override application.properties with the -D options.
As it uses an embedded tomcat you should be able to add a /META-INF/context.xml to each application which specifies the path (at least this should work for a normal tomcat).
That works for our normal embedded tomcat stuff, so I would expect it to work for Spring Boot as well.

Categories

Resources