Java app working when running spring boot but not with Apache - java

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

Related

HTTP Post method not supported by this URL when deployed to jboss EAP 7.1

I've a REST API, that is deployed on JBoss EAP 7.1. When I hit the URL at
http://localhost:8080/MyApp/group
in postman, it gives "HTTP method POST is not supported by this URL" error with status code 405.
When I deploy this API on embedded tomcat server, it works perfectly fine. Here is my controller
#RestController
public class RequestController {
#Autowired
private GroupService groupService;
#PostMapping( "/group" )
public GroupInfo fetchGroupInfo( #RequestBody GroupInfo groupInfo ) {
long groupId = groupInfo.getGroupId();
return groupService.getGroup( groupId );
}
}
main class
#SpringBootApplication
public class MyApp extends SpringBootServletInitializer {
/**
* #param args
*/
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
return builder.sources(MyApp.class);
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.package</groupId>
<artifactId>MyApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyApp</name>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<dependencies>
<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-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<finalName>MyApp</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The deployment shows no error messages. However, it does show below warning
WARN [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0013: Deployment optumRx-0.0.1-SNAPSHOT.war contains CDI annotations but no bean archive was found (no beans.xml or class with bean defining annotations was present).
And the class files are missing in the deployment folder in jboss. Can you please tell, what am I missing?
About path: construction in pom.xml < finalName> MyApp < / finalName> doesn't says about way in url, it's about name of artifact.
Try to use http://localhost:8080/group (without MyApp)
About other: if you want deploy on jboss, you must exclude tomcat in pom.xml:
<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>
About CDI warning - try to use #ComponentScan(basePackages="your.package") on MyApp class
Looks like I missed the servlet api dependency in my project. After I add that dependency everything is working fine. The embedded Tomcat server have this jar and so, it was not needed when I deployed on Tomcat.

Spring Boot application runs on localhost but returns 404 on external Tomcat

I have created a Spring Boot application, with Spring Security, to expose some REST API endpoints.
I've created the project using the built in IntelliJ Ultimate Spring Initializr.
Spring version: 2.4.4
Type: Maven
Java version: 8
Packaging: War
When I run the application from within IntelliJ and it starts an instance of Tomcat 9.0.44.
Then I created a WAR file, using maven>package, to test in on my PC's local XAMPP installation. (I use Tomcat 8.5.53. The reason is that this is the Tomcat version that is available at our production environment to which I finally want to deploy.)
I deployed using the tomcat manager, and it runs without any problem there as well.
I then moved the war to our production server and used the Tomcat manager UI to deploy.
I can see in the manager that it deployed and that it is running.
Production server Tomcat Manager UI
When I tried to access my endpoints, using Postman, no matter what endpoint I try to access I always get a 404 error...
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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.apiauthentication</groupId>
<artifactId>auth-server</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<name>FLEET_Authentication_Server</name>
<description>Authentication Server</description>
<properties>
<java.version>1.8</java.version>
<jjwt.version>0.10.6</jjwt.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.4.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>${artifactId}</finalName>
</build>
</project>
main class-AuthServerApplication
#SpringBootApplication
public class AuthServerApplication {
public static void main(String[] args) {
SpringApplication.run(AuthServerApplication.class, args);
}
}
Below are the entries in the Tomcat logs
catalina.log
21-Apr-2021 08:14:09.860 INFO [http-nio-8080-exec-2726] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /opt/tomcat/webapps/auth-server.war
21-Apr-2021 08:14:11.828 INFO [http-nio-8080-exec-2726] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
21-Apr-2021 08:14:16.257 INFO [http-nio-8080-exec-2726] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /opt/tomcat/webapps/auth-server.war has finished in 6,397 ms
localhost.log
21-Apr-2021 08:14:11.839 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath
21-Apr-2021 08:14:13.419 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
manager.log
21-Apr-2021 08:12:04.977 INFO [http-nio-8080-exec-2623] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host 'localhost'
21-Apr-2021 08:14:09.806 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log HTMLManager: install: Installing web application '/auth-server' from '/tmp-war/auth-server.war'
21-Apr-2021 08:14:16.257 INFO [http-nio-8080-exec-2726] org.apache.catalina.core.ApplicationContext.log HTMLManager: list: Listing contexts for virtual host 'localhost'
In the localhost_access_log file I can see no entries for my attempts to access the newly deployed application.
Some things that I have tried:
Extend SpringBootServletInitializer on my main class and override the configure method
#SpringBootApplication
public class AuthServerApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(AuthServerApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AuthServerApplication.class);
}
}
Add start-class in the properties section of the pom.xml
<properties>
...
<start-class>
com.apiauthentication.authserver.AuthServerApplication
</start-class>
</properties>
Add an exclusion to the spring-boot-starter-web, for spring-boot-starter-tomcat
<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>
And different combinations of those things as well
Anyone has any hints as to what might be the problem?
Basically I can run on my local XAMPP but not on the production server
Adding the comment from Piotr P. Karwasz as the answer.
What seems strange is the lack of entries in the access log. Are your sure that your production server is not behind a reverse proxy?
The problem was that I was trying to access the application using the domain name, but since the domain was managed using Plesk, and there are other domains on that server as well, I had to go to the Apache settings of the domain and add some new ProxyPass and ProxyPassReverse directives, for HTTP and HTTPS

After deploying war file in Tomcat api not working

I'm trying to deploy app in my local tomcat but have some problems. I use: Tomcat 9, Spring boot, ReactJS and Webpack. When I run embedded Tomcat (in Eclipse) all be ok - API working good, but when I build war file and paste it to my local Tomcat - API not working, all request failed.
How I build war file:
run mvn "clean install";
paste war file to my local Tomcat directory "webapps";
wait for deploying;
go to "http://localhost:8080/web_importer/#/importCandidates". But in this moment when i run in Eclipse my app i use another path: "http://localhost:8080/#/importCandidates" and API use this path to (example) "http://localhost:8080/api/originator", not "http://localhost:8080/web_importer/api/originator".
How I can change this path?
application.properties:
server.port = 8080
management.server.port: 8995
management.server.address: 127.0.0.1
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
spring.http.encoding.force=true
OriginatorController.java:
#RequestMapping(value = "/api/originator", method = RequestMethod.GET)
public List<OriginatorModel> retrieveOriginators() {
logger.info("Performing /api/originator GET request");
return originatorService.retrieveOriginators();
}
My pom.xml:
<groupId>com.kddb_web_importer</groupId>
<artifactId>web_importer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>web_importer</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
When you start the application from Eclipse, it uses a standalone server, which is directly accessed via localhost and the URL is http://localhost:8080/api/originator.
When you run your local tomcat instance, http://localhost:8080 is the base tomcat URL. What follows next is the name of your application, in this case web_importer. So the URL becomes http://localhost:8080/web_importer/api/originator and this is why you get 404 Not Found.
It seems that your frontend is calling the API directly at http://localhost:8080/api/originator. You need to change your base API URL in your frontend configuration when you want to use the tomcat-deployed version of your API.

Spring boot blank page response on browser

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.

H2-In memory database console not opening

I am using the H2 database in a Spring boot application. But unable to open it in the browser at http://localhost:8080/console. My pom.xml is as below:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>
Spring boot Configuration :
Springboot configuration file
#Configuration
public class WebConfiguration {
#Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
}
to use the H2 console you need to configure it in your .properties file
spring.h2.console.enabled=true
spring.h2.console.path=/h2console/
where /h2console/ is the path you want to use on the browser so you can change it to anything. Also if you have security enabled you might want to add it to the permitted paths
also add this to your HttpSecurity configuration http.headers().frameOptions().disable();
Edit
change your security configuration i'm pretty sure you might have spring security in your pom so use this instead, if not it should work
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
#Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
.authorizeRequests().antMatchers("/console/**").permitAll();
httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}
}
If have included spring-boot-starter-security artifact in your pom then by default basic authentication is enabled. Hence, to access your console either you disable the basic authentication by adding security.basic.enabled=false in your application.properties or allow the access in your configure method as below:
#Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable().authorizeRequests().antMatchers("/").permitAll().and().authorizeRequests()
.antMatchers("/console/**").permitAll();
httpSecurity.headers().frameOptions().disable();
}
}
Thank you all for your generous help.The application class (Springboot) was in a separate package and it was not scanning other packages.Also I modified my Pom.xml a bit which finally helped me to access the console.Attached is my new 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>com.spring.app</groupId>
<artifactId>Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootApp</name>
<description>Generator of statistics </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>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<!--WebJars -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring AOP + AspectJ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!-- JavaConfig need this library -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
go the POM file and add the dependency :
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
rebuild your project
this might help folks , all the configuration above is correct
Note - if you are not using any security then adding spring security is not required
Actualy problem is - when you open this url in chrome
http://localhost:8080/h2 chrome makes it --> https://localhost:8080/h2
To get rid of this issue - Below reference will help -
Google Chrome redirecting localhost to https
You might have face 2 situation including following errors:
localhost refused to connect
Double check the URL. Chrome automatically try to change http:// to https://
Check spring.h2.console.path (Path at witch the console avilible) to get your URL:
Default: /h2-console --> URL: http://localhost:8080/h2-console/
If you running IDE (e.g. IntelliJ Idea), make sure your app is running witch means your H2 data base is running!
You face 404 Error:
In this case your H2 Data base is correctly running on Port 8080 and you already have the connection with it.
Check spring.h2.console.path (Path at witch the console avilible) to get your URL:
Default: /h2-console --> URL: http://localhost:8080/h2-console/
Enable H2 Console
spring.h2.console.enabled=true
The issue might be also be caused by adding server.servlet.context-path to properties. The new url will be composed of server.servlet.context-path plus spring.h2.console.path
If you have renamed the h2 path in the application.properties to "console" you've to add it in your antMatcher like .antMatcher("/console/**") with two asterisks after the "console" because there are much more appendings.
If none of the above solution works, try below one, this worked for me :
Add below property in your application.propertiesfollowing
spring.data.jpa.repositories.bootstrap-mode=default
Open console in browser using this URL : http://localhost:8080/h2-console
On a login page make sure that you use jdbc:h2:mem:testdb as JDBC URL.
As h2 database console is mapped to "h2-console".
Use this:
http.csrf().disable().authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated();
// disable frame options
http.headers().frameOptions().disable();
`
You don't need permit root access:
.antMatchers("/") * NOT NEEDED *

Categories

Resources