Spring boot blank page response on browser - java

I have a web application using maven & spring boot with a war deployment.
While it works normally on a windows machine (starting it using netbeans), I get a blank page response from browser when I run it on a linux machine (not sure if this is relevant). No errors occur on startup.
After searching a while I found that it is related with some 404 error response of spring boot. This happens for any route that I try (valid or not)
Another clue is that I tried to redirect 404 errors to a test.jsp but nothing changed in the browsers (I still get this blank page). With postman I get this .jsp as a response.
In any case it's not normal to get an error response since the routes are correct.
Here is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=`"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">`
<modelVersion>4.0.0</modelVersion>
<groupId>org</groupId>
<artifactId>app</artifactId>
<version>1.0-RELEASE</version>
<packaging>war</packaging>
<name>webapp</name>
<description></description>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!--<failOnMissingWebXml>true</failOnMissingWebXml>-->
<webResources>
<resource>
<directory>${basedir}/src/main/resources/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Any ideas?
EDIT
I add some extra info that might help:
No MVC-relevant configuration is included in application.properties
Application.java (I'm not using #EnableWebMvc):
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
Web MVC Configuration is done by:
#Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver getPageViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
resolver.setOrder(1);
return resolver;
}
}

After some tries I conclude that:
mvn spring-boot:run
java -classpath "lib/*:classes/." org.app.Application
java -jar webapp-1.0.RELEASE.war
start the app as a normal application which leads to this problem. No matter if pom packing is defined as jar or war (If I'm wrong someone pls correct me)
Netbeans was deploying the app into an external tomcat server. I finally tried the classic war deployment way in order to make it work.
This answer is not solving the original question but provides a fix in case someone else has the same problem.

Related

Java app working when running spring boot but not with Apache

I created an app using https://start.spring.io/ so when I run ./mvnw spring-boot:run the apps work great. But then in order to be able to debug my app (as I always did with others) I select "Debug On Server" and the server compile successfully with the message INFORMACIÓN: Server startup in 4288 ms
But when I try to access to the app I get 404 error.
This is my pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.domain.app</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>app</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Thhis is my controller:
#RestController
#RequestMapping(
value = "/api/products",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public class ProductController {
#Autowired
ProductService productService;
#GetMapping("")
#ResponseStatus(HttpStatus.OK)
public String getAllProducts() {
return this.productService.initCrawler();
}
}
And the URL I'm pointing is:
http://localhost:8080/app/api/products
And the response is HTTP 404
Also eclipse after the build open the explorer in http://localhost:8080/app but it gets also 404.
When running directly with ./mvnw spring-boot:run the URL that works fine is http://localhost:8080/api/products which is not working neither when I use Run as Server
And in the logs doesn't say anything...
I understood your question this way. You have created a spring boot application from Spring Initializr and the application run successfully from command line but not when you are trying to deployed in server and start the server in debug mode.
You should know that Spring Initializr created a spring boot application which is stand-alone, production-grade Spring based Applications that you can "just run". With stand-alone meaning the the server is incorporated as dependency in spring boot app and you don't need to deployed in server.
If you want to deploy in server , you need to make Spring Boot Application #SpringBootApplication class extend the SpringBootServletInitializer class.
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
You should specify in maven the main class that is going to run and change the packaging from jar to war.
<packaging>war</packaging>
<start-class>com.tutorialspoint.demo.DemoApplication</start-class>
For more information how to make you spring boot application to be deployed in server, check the link : https://www.tutorialspoint.com/spring_boot/spring_boot_tomcat_deployment.htm
If you don't need the application to run on the server , you just want to debug the code. I would suggest to use eclipse or intellij. You include spring boot dashboard in perspective and start the spring boot application in debug mode. Check this link to understand how to include and use spring boot dashboard : https://medium.com/danielpadua/java-spring-boot-eclipse-744454468670

REST end point of Spring Boot application as WAR on tomcat with keycloak is no more secure

I have a spring - boot application with tomcat embedded and I configures my application with keycloak-spring-boot-adapter according to the below link
Spring boot adapter
I packaged my application as Jar and every thing works perfectly. My context root was
localhost:8080/service/api/*
and there was no problem, All end points was secure. Without authentication no one can access the API. Here is the Properties
server.contextPath=/service
keycloak.realm = demo
keycloak.resource = client-apps
keycloak.auth-server-url = http://localhost:8180/auth
keycloak.ssl-required = external
keycloak-bearer-only = true
keycloak.credentials.secret = client-apps
keycloak.public-client = true
keycloak.enabled = true
keycloak.use-resource-role-mappings = true
keycloak.securityConstraints[0].authRoles[0] = admin
keycloak.securityConstraints[0].securityCollections[0].name = Secure Mappings
keycloak.securityConstraints[0].securityCollections[0].patterns[0] = /api/*
keycloak.securityConstraints[0].securityCollections[0].patterns[1] = /service/api/*
keycloak.securityConstraints[0].securityCollections[0].patterns[1] = /*/api/*
Now Requirement Changes and we need to deploy the Jar as War on Tomcat. Here is the POM
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>group</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>${packaging.type}</packaging>
<name>Loan Service</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.version>4.12</junit.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>
<!-- <dependency> <groupId>io.github.benas</groupId> <artifactId>random-beans</artifactId>
<version>3.7.0</version> </dependency> -->
<dependency>
<groupId>org.jsondoc</groupId>
<artifactId>jsondoc-core</artifactId>
<version>1.2.19</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.keycloak.bom</groupId>
<artifactId>keycloak-adapter-bom</artifactId>
<version>3.4.3.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>jar</id>
<properties>
<packaging.type>jar</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>war</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- <configuration>
<webResources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration> -->
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
War is successfully made and After deploying on the tomcat webapps directory , my REST end points are no longer secure. Now the Root context path is same as before
localhost:8080/service/api/*
AS you can see in properties file that which paths needs to be secure. Now I have tried but can't figure out what is the root cause of this?
Can anybody expert in keycloak and spring boot help me that what i ma missing or where i am doing wrong.
Project Structure:
Spring Boot Config:
#SpringBootApplication
public class Application extends SpringBootServletInitializer{
#Autowired
private MessageSource messageSource;
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.properties("spring.config.name=application,master-data")
.run(args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class).properties("spring.config.name=application,master-data");
}
#Bean
#Scope(scopeName = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public AccessToken getAccessToken() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();
return ((KeycloakPrincipal<?>) request.getUserPrincipal()).getKeycloakSecurityContext().getToken();
}
#Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource);
return bean;
}
}
the problem is that your new Root context: host:port/service/api/* doesn't match up with your Rest Endpoints being in /api/ . Does that make sense? You have that extra path [port] in your context. that's what's throwing it off.
My suggestion is to switch to the Spring Security Adapter instead. With this adapter, you can do everything the Spring Boot Adapter does (and much more), in a more flexible way. You only need to write some configuration code.
Related to your question, here you've got one thread that discusses a problem similar to the one of yours, which was brought to a JIRA issue (I can't find the link, though). It is couple of years old, but the KC team member states they haven't even tried that scenario (Spring Boot Adapter + War deployment).

jboss eap 6.4 in maven access failed

I had deploy maven project built in war package to Jboss EAP 6.4 and successfully, WAR package deployed. I had created services and run with spring-boot in local eclipse and I can ran in browser but I have a problem when access that URL based on war package in Jboss eap. I success ran jboss service but cannot run the URL. for example, I have service with method GET with url: localhost:8080/letter-printing-eap-generator/testing cannot run in jboss but ran in local eclipse before deploy. how to fix this problem? any configuration xml in maven project? I just add jboss plugin. thanks. my code:
my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.prudential.letter.printing</groupId>
<artifactId>letter-printing-eap-generator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>LetterPrintingEapGenerator</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>1.2.2.RELEASE</spring-cloud.version>
<swagger.version>2.6.1</swagger.version>
<jboss.home>${env.JBOSS_HOME}</jboss.home>
<config.server>http://10.170.49.103/configserver</config.server>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- JBOSS maven plugin to simulate deployment to JBOSS -->
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.9.Final</version>
<configuration>
<jbossHome>${jboss.home}</jbossHome>
<serverArgs>
<serverArg>-Dspring.profiles.active=${run.profiles}</serverArg>
<serverArg>-Dspring.cloud.config.uri=${config.server}</serverArg>
</serverArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
SpringBootRestApiApp.java
#SpringBootApplication(scanBasePackages={"com.prudential.letter.printing"})
#Import({SpringDataRestConfiguration.class})
public class SpringBootRestApiApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestApiApp.class, args);
}
}
my controller:
#RestController
#RequestMapping(value="/")
public class TestingController {
#GetMapping("testing")
public String getTestingMethod(){
return "Hello Testing";
}
#GetMapping("data")
public Map<String, Object> getData(){
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", "200");
map.put("message", "ini messagenya");
map.put("content", "mantap");
return map;
}
}
this is my project structure:
application.yml :
server:
port: 8080
contextPath: /letter-printing-eap-generator
In the absence of any specific configuration, JBoss EAP will provide access to your web application at a context with a name that matches the WAR file name.
Therefore, your application should be accessible at:
http://localhost:8080/letter-printing-eap-generator-0.0.1-SNAPSHOT/testing
A common way of changing this for development purposes is to include a finalName element in the pom.xml file:
<build>
<finalName>${project.artifactId}</finalName>
...
</build>
This will generate a WAR file named letter-printing-eap-generator.war and the original URL that you tried should work.
Alternatively, you could add a jboss-web.xml file to your deployment (in the WEB-INF directory) that contains a context-root element:
<jboss-web>
<context-root>letter-printing-eap-generator</context-root>
</jboss-web>
You could also do this using the JBoss CLI or the web console to perform deployment as well.

Springboot app web page returns 404 when deployed to production

I have a springboot app that serves a webpage just fine when running locally but for some reason returns 404 when I package a deploy the war file to production env, I get a 404 error. In my controller, I return a jsp page that includes an html page from my /resources/static folder. This all works fine locally.
I'm deploying to tomcat 7.0.52
this is my pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>4.3.9.RELEASE</spring.version>
<java.version>1.8</java.version>
<maven-compiler.version>3.6.1</maven-compiler.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- //////////////////////////////////////////////////////// -->
<!-- SPRING -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.19</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>
repackage
</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
`
My Springboot app's main class
#SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringbootApplication.class, args);
}
}
My class that extends webmvcConfigureAdapter
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
#Override
public void addViewControllers(ViewControllerRegistry registry) {
System.out.println("in mvc config");
registry.addViewController("/").setViewName("forward:index2.html");
}
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
System.out.println("configure serve handling");
configurer.enable();
}
#Bean
InternalResourceViewResolver internalResourceViewResolver () {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
My main controller.
#Controller
public class MainController {
#RequestMapping(value={"/"}, method = RequestMethod.GET)
public String index(){
System.out.println("inside mainController");
return "chatbot";
}
}
Finally my application.properties file looks like this.
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
The structure of my project looks like this...
src/main/webapp
-> assets
-> resources
-> static ->index.html
-> WEB-INF
->jsp -> index.jsp
Please help. I've been stuck on this for over 8 hrs now.
Thanks.
YOu can place your index.html under webapp since this is the default contextpath, all other resources should be relevant to it.
Maybe the problem is that you have to include the application context path.
Check the following link to do that add-context-path-to-spring-boot-application

Unable to access and use static resources in Spring Boot Application

I have been fighting with getting resources to work for quite sometime now, and I know there are thousands of similar questions.. Anyway, my condition is this
I will start with my pom.. I might have missed something here?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ruruapps</groupId>
<artifactId>spring-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-demo</name>
<description>Spring-Demo Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.thymeleaf-version>2.1.4</org.thymeleaf-version>
<webjars-bootstrap.version>3.3.5</webjars-bootstrap.version>
<webjars-jquery-ui.version>2.1.1</webjars-jquery-ui.version>
<webjars-jquery.version>2.0.3-1</webjars-jquery.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Thymeleaf -->
<!--<dependency>-->
<!--<groupId>org.thymeleaf</groupId>-->
<!--<artifactId>thymeleaf</artifactId>-->
<!--<version>2.1.4.RELEASE</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Webjars (static dependencies distributed as WAR files) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here are the errors I am dealing with
And in the terminal I see this
Looking up handler method for path /css/bootstrap/css/bootstrap.min.css
Did not find handler method for [/css/bootstrap/css/bootstrap.min.css]
Request method 'GET' not supported
What does this mean?
I have a correct structure and doing it by the convention
But no luck what so ever. I tried changing to public/ and tried using webjars. All in vain. What are the norms for this and how do I get this set up so it wont be breaking! Thanks!
EDIT:: Configuration Files
Here is my application file that is provided with the spring boot
#SpringBootApplication
#EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})
#ComponentScan(basePackages = {"webapp"})
#EnableJpaRepositories
public class SpringDemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringDemoApplication.class, args);
}
}
And I have a WebConfig file which is mostly empty, but I tried adding the #Overide on addResourceHandlers, with no improvements
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"webapp"})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
}
}
EDIT
After further examining the application, I have found this
It says that the request it allows is PUT, but why? From what I can understand is that something is blocking my request to the resource files, is that right?
Desperate to get this fixed, it's really bothering to configure Spring, even with Spring Boot!
Found a problem that I was facing with not being able to access static resources.
For those who experience the same:
This behaviour (405 error) signifies that something is blocking the path to your static resources. This doesn't have to be /static or /public. It wont do anything.
In my case the problem was that a controller was blocking the /css path. To fix this a #RequestMapping(value = "/Somepath") annotation on the class lvl is needed to release the path for the static resources.
This worked for me and now I can access my static resources without any issues.
This can be a very annoying problem and someone can spend hours and days trying to figure this out. Hope this helps others to save their time
If there are alternative or other methods of preventing this behaviour that please mention in the comments
If you are trying to configure resource mapping through configuration class as you mentioned in comment, then it should be like this:
#EnableWebMvc
#ComponentScan(basePackages = {"org.company.yourpackage"})
#Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
}
}
this post also may help.
I was having the exact same issue, What helped me was adding:
enter code here
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
OR
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
from this post
servlet for serving static content here

Categories

Resources