Spring Boot - Proper location for html files - java

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

Related

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.

How to deploy the Spring boot with angular2 code in heroku

How to deploy the Spring boot with angular2 code in heroku.. i have front end and back end code in different folder
I don't understand your problem)
If you want to deploy front and back ends together to Tomcat (for example), you should just create static folder in resources and transfer to it all your js code.

Camunda Spring Boot starter with embedded forms

I am trying to use embedded forms with a start event with the Camunda spring boot starter.
My startEvent is described like this:
<bpmn:startEvent id="StartEvent_1" name="Rechnungseingang" camunda:formKey="embedded:app:forms/rechnungseingang.html">
<bpmn:outgoing>SequenceFlow_0dtfc1a</bpmn:outgoing>
</bpmn:startEvent>
The form itself is located under "src/main/webapp/forms/rechnungseingang.html", from my understanding this should be the correct path.
If I try to start the process after starting the spring boot app, I am receiving the error: "Form failure: The context path is either empty or not defined."
In the browser console, I can see a request to http://localhost:8080/test/api/engine/engine/default/process-definition/Rechnungseingang:1:927f0aa4-e590-11e7-973d-e2cbd8678b9f/startForm with the response:
{"key":"embedded:app:forms/rechnungseingang.html","contextPath":null}
Obviously the application can't handle the null value in the contextPath. How am I able to set the contextPath for Camunda in Spring Boot? In the application.properties I already tried to set server.context-path with no effect.
1.) there is no src/main/webapp with spring boot applications, use src/main/resources/static
2.) for camunda to link the resource to the engine, you will need a process application. This is done easily by adding "#EnableProcessApplication" to your spring boot app.
3.) Autodeployment requires a src/main/resources/META-INF/processes.xml file, but you can leave it empty
4.) there is a full example for embedded forms with camunda spring boot here: https://github.com/camunda/camunda-bpm-examples/tree/master/spring-boot-starter/example-twitter

Spring Boot static content url mapping

Is anybody here who know how to create a mapping file from static directory to respond for certain url in Spring Boot?
For example, I have file in directory /resource in Spring Boot structure
resources/static/html/index.html
and I want it to respond to url
/index
No catalogue path, no .html postfix
File index.html is served as default HTML file from certain directory. So if you want to serve it from http://domain:port/index URL, you should place it into resources/static/index/index.html.
As I research more, url mapping is possible only if You use template engine like Thymeleaf and then application will became context aware.
Use Thymeleaf and then add your .html files to resources/templates/ and they will be discovered automatically.

How to use two URL mappings

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{

Categories

Resources