So this is my first web project with spring boot and Gradle, and I have done the same steps as mentioned in the get started with spring boot.
Even though the building process is ok and the dependencies seems to be also ok, however whenever I call the controller /firstapp I get a null view, even if I tried to return a string I have a Whitelabel Error Page.
This is my project.
Project directory picture:
Test controller :
main class :
Application.properties :
build.gradle :
Try the *.html in the spring.mvc.view.suffix property instead of .html
The problem was due to my bad directory configuration, the spring-boot could not read the Viewresolver from the application.properties and also the controller class should be in a subdirectory under the main springapplication.
Related
Good day,
I am doing a Spring Boot Application in my Eclipse IDE. When I right click on my SpringBoot Application file and run as Java application, I hitting error as follow:
APPLICATION FAILED TO START
Description:
Field tutorialRepository in com.utility.tool.ToolApplication required a bean of type 'com.utility.tool.repository.TutorialRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.utility.tool.repository.TutorialRepository' in your configuration.
Then I found that I forget to include the spring boot starter data jar. Hence, I add the following code in my build.gradle and it finally run correctly:
implementation 'org.springframework.boot:spring-boot-starter-data-jpa:2.7.5'
Then I right click my project and export jar as runnable jar, and then try to run it by java -jar my.jar, and it hit back the error.
I open the jar in JdGui, and found that the spring-boot-starter-data-jpa-2.7.5.jar is inside. May I know what is my mistake? My jar structure is something as follow:
The jar is in the list but is at bottom, thus not in my screen shot.
Check your SpringBoot annotations. You may be missing some #Service, #Repository, #Component annotations.
I have been using the below property in the application.properties file with spring-boot.version 1.5.6.RELEASE without any issues.
server.servletPath=/*
This was a workaround to enable a method in a library class which uses the function getPathInfo() of javax.servlet.http.HttpServletRequest to get a valid value instead of null.
I had to go with this workaround since there is no support of that library jar anymore.
This workaround started failing when I upgraded my application to spring-boot.version 2.1.7.RELEASE
server.servletPath is changed to spring.mvc.servletPath from Spring Boot 2 onwards.
I tried setting the below property and it did not work
spring.mvc.servletPath=/*
I also tried the below function in my configuration class and it did not work.
#Bean
public DispatcherServletRegistrationBean dispatcherServletRegistration(
DispatcherServlet dispatcherServlet,
ObjectProvider<MultipartConfigElement> multipartConfig) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
dispatcherServlet, "/*");
registration.setName("dispatcherServlet");
registration.setLoadOnStartup(-1);
multipartConfig.ifAvailable(registration::setMultipartConfig);
return registration;
}
Could you please provide a working solution for this property using spring-boot.version 2.1.7.RELEASE?
Thanks,
Dhinu
The correct setting for newer spring versions is:
spring.mvc.servlet.path=/some/path
This changes the mapping of the DispatcherServlet, so all resources served by spring are mapped to this path.
If you set:
server.servlet.contextPath=/some/path
The whole web context is changed.
The main difference is that setting the dispatcher servlet path allows you to register additional servlets on other paths while with context path set, spring boot's tomcat can only serve content below that context path.
Use the following config property on latest spring boot version:
server.servlet.contextPath=/*
According to Spring Boot Documentation
By default Spring Boot will serve static content from a directory called /static in the classpath or from the root of the ServletContext
I've set up spring.mvc.static-path-pattern=/resources/** in the application.properties - nonetheless I get an error 404.
(No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcherServlet')
Here is my project hierarchy:
What am I missing?
Solution:
I had a Controller Mapping to /, after removing it I could see the website! facepalm
Did you try it without the spring.mvc.static-path-pattern=/resources/**? Or try to access /static/index.html with the property set? Because that setting changes where Spring Boot would look for static files, and I clearly see your index.html being in resources/static folder.
I have a Spring boot project which is packaged into jar and it functionalities are related to MQ and database. I want to now host a webservice on that project however I cannot use #EnableAutoConfiguration or #SpringBootApplication. The webservice to be created is a simple GET service that will return a integer value. I have tried following steps but still the webservice doesnt start.
Added Spring-webmvc, spring-web, spring-boot-web ,spring-tomcat jars to pom.xml
Changed the artifact type from jar to war in pom.xml
Added #EnableWebMvc in the config class
Added a class with #Controller and #RequestMapping
Created a bean for EmbeddedServletContext for Tomcat.
What have I missed?
Some blogs mentioned about creating a WebApplicationContextInitializer and some mentioned about creating a Dispatcher servlet.
My question is what all beans do I need to manually create to bring up my webservice (as I cannot use EnableAutoConfig)
I have tried to run "Converting a Spring Boot JAR Application to a WAR" application by converting into a war package as instructed...and since it was mentioned that void main() is no longer needed,I removed it and tried to build it using gradle but it throws error unable to find main class.
The content of class after my modification is as below
Application.java
#Configuration
#EnableAutoConfiguration
#ComponentScan
public class Application {
}
what is the mistake am I making?
If you don't want an executable war then remove the Spring Boot plugin. If you do, leave it in, and keep the main().