Spring Boot Reference Guide - get 404 from app.groovy example - java

I follow Spring Boot Reference Guide and got this error
HTTP Status [404] – [Not Found]
Type Status Report
Message /
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Here is the code
#RestController
class ThisWillActuallyRun {
#RequestMapping("/")
String home() {
"Hello World!"
}
}
Command that I run.
spring run app.groovy
Console log that I got.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.4.RELEASE)
2017-07-21 14:37:37.474 INFO 47637 --- [ runner-0] o.s.boot.SpringApplication : Starting application on MacBook-Pro-2016.local with PID 47637 (started by moss in /Users/moss/java_ws/hello)
2017-07-21 14:37:37.477 INFO 47637 --- [ runner-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default
2017-07-21 14:37:37.659 INFO 47637 --- [ runner-0] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#3a0184b7: startup date [Fri Jul 21 14:37:37 ICT 2017]; root of context hierarchy
2017-07-21 14:37:38.831 INFO 47637 --- [ runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-21 14:37:38.844 INFO 47637 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-21 14:37:38.846 INFO 47637 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-21 14:37:38.923 INFO 47637 --- [ost-startStop-1] org.apache.catalina.loader.WebappLoader : Unknown loader org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader#c0454ed class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader
2017-07-21 14:37:38.950 INFO 47637 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-21 14:37:38.951 INFO 47637 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1296 ms
2017-07-21 14:37:39.020 INFO 47637 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-21 14:37:39.605 INFO 47637 --- [ runner-0] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-21 14:37:39.674 INFO 47637 --- [ runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-21 14:37:39.678 INFO 47637 --- [ runner-0] o.s.boot.SpringApplication : Started application in 12.556 seconds (JVM running for 14.572)
Did I do something wrong?

I just use sdkman switch back to Spring CLI v1.5.3.RELEASE and it's work.

Related

I deployed my spring boot project with jar file on my server and can't access it

I deployed my spring boot project with jar file on my lightsail server. I think it deployed fine however, I can't access it.
Chrome says,
This site can’t be reached {ip} refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
internal tomcat log
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.0)
2022-06-21 05:37:40.476 INFO 7028 --- [ main] com.--Application : Starting MyApplication using Java 17.0.3 on ip-172-26-7-171 with PID 7028 (/home/ubuntu/My.jar started by root in /home/ubuntu)
2022-06-21 05:37:40.483 INFO 7028 --- [ main] com.--Application : The following 1 profile is active: "prod"
2022-06-21 05:37:42.758 INFO 7028 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-06-21 05:37:42.883 INFO 7028 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 100 ms. Found 1 JPA repository interfaces.
2022-06-21 05:37:44.472 INFO 7028 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 80 (http)
2022-06-21 05:37:44.501 INFO 7028 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-06-21 05:37:44.502 INFO 7028 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.63]
2022-06-21 05:37:44.694 INFO 7028 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-06-21 05:37:44.695 INFO 7028 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3508 ms
2022-06-21 05:37:45.661 INFO 7028 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-06-21 05:37:46.006 INFO 7028 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-06-21 05:37:46.121 INFO 7028 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-06-21 05:37:46.275 INFO 7028 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.9.Final
2022-06-21 05:37:46.658 INFO 7028 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-06-21 05:37:46.910 INFO 7028 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
2022-06-21 05:37:48.039 INFO 7028 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-06-21 05:37:48.055 INFO 7028 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-06-21 05:37:48.995 WARN 7028 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-06-21 05:37:49.637 WARN 7028 --- [ main] org.thymeleaf.templatemode.TemplateMode : [THYMELEAF][main] Template Mode 'HTML5' is deprecated. Using Template Mode 'HTML' instead.
2022-06-21 05:37:50.173 INFO 7028 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter#3d1f558a, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#6abdec0e, org.springframework.security.web.context.SecurityContextPersistenceFilter#3762c4fc, org.springframework.security.web.header.HeaderWriterFilter#4b4ee511, org.springframework.security.web.csrf.CsrfFilter#38f77cd9, org.springframework.security.web.authentication.logout.LogoutFilter#2ae62bb6, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#5762658b, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter#6ca372ef, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter#28f4f300, org.springframework.security.web.authentication.www.BasicAuthenticationFilter#6aa3bfc, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#59fbb34, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#1b6924cb, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#2b5c4f17, org.springframework.security.web.session.SessionManagementFilter#5a034157, org.springframework.security.web.access.ExceptionTranslationFilter#4483d35, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#6fc1020a]
2022-06-21 05:37:50.300 INFO 7028 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 80 (http) with context path ''
2022-06-21 05:37:50.332 INFO 7028 --- [ main] com.--Application : Started MyApplication in 10.965 seconds (JVM running for 12.248)
This is my firewall settings on lightsail.
/home/ubuntu# lsof -i -nP | grep LISTEN | awk '{print $(NF-1)" "$1}' | sort -u
*:22 sshd
127.0.0.1:33060 ssh
127.0.0.1:80 java
127.0.0.53:53 systemd-r
Do I have to edit firewall setting?
I remove server.address=localhost from application.properties and it works well.

Injecting JdbcOperationsSessionRepository into a constructor

I'm upgrading a SpringBoot app from 2.1.17 to 2.3.4, which includes this class:
#Configuration
public class SessionRepositoryConfig {
public SessionRepositoryConfig(JdbcOperationsSessionRepository sessionRepo) {
// configure session expiry + table name + converter
...
...
}
}
This is the only mention of JdbcOperationsSessionRepository in the entire code-base, so presumably in 2.1.17 a bean of this type was automatically made available by SpringBoot. However, when I switch to 2.3.4 the injection fails:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.4.RELEASE)
2020-10-13 12:32:43.970 INFO --- [kground-preinit] o.h.v.i.u.Version : HV000001: Hibernate Validator 6.1.2.Final
2020-10-13 12:32:44.082 INFO --- [ main] c.s.o.a.s.UIAuthorizationEndpointTest : Starting UIAuthorizationEndpointTest on UK-WMorgan1 with PID 17444 (started by WMorgan in C:\git_repos\oauth2-auth-server\sc-oauth2-authorization-server-application)
2020-10-13 12:32:44.086 INFO --- [ main] c.s.o.a.s.UIAuthorizationEndpointTest : The following profiles are active: local,dbAutoUpdate,localLDAP
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/C:/Users/wmorgan/.m2/repository/org/codehaus/groovy/groovy/2.5.13/groovy-2.5.13.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2020-10-13 12:32:47.823 INFO --- [ main] o.s.b.w.e.t.TomcatWebServer : Tomcat initialized with port(s): 0 (http)
2020-10-13 12:32:47.848 INFO --- [ main] o.a.c.h.Http11NioProtocol : Initializing ProtocolHandler ["http-nio-auto-1"]
2020-10-13 12:32:47.849 INFO --- [ main] o.a.c.c.StandardService : Starting service [Tomcat]
2020-10-13 12:32:47.849 INFO --- [ main] o.a.c.c.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-13 12:32:48.046 INFO --- [ main] o.a.c.c.C.[.[.[/oauth2Server] : Initializing Spring embedded WebApplicationContext
2020-10-13 12:32:48.046 INFO --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3561 ms
2020-10-13 12:32:48.444 INFO --- [ main] c.t.c.c.f.KeyStoreFactoryBean : Loading key store from URL [file:./src/test/resources/thunderhead.jks]
2020-10-13 12:32:48.809 INFO --- [ main] c.z.h.HikariDataSource : HikariPool-1 - Starting...
2020-10-13 12:32:49.120 INFO --- [ main] c.z.h.HikariDataSource : HikariPool-1 - Start completed.
2020-10-13 12:32:50.376 WARN --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sessionRepositoryConfiguration' defined in file [C:\git_repos\oauth2-auth-server\sc-oauth2-authorization-server-application\target\classes\com\SessionRepositoryConfig.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.springframework.session.jdbc.JdbcOperationsSessionRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-10-13 12:32:50.377 INFO --- [ main] c.z.h.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-10-13 12:32:50.385 INFO --- [ main] c.z.h.HikariDataSource : HikariPool-1 - Shutdown completed.
2020-10-13 12:32:50.393 INFO --- [ main] o.a.c.c.StandardService : Stopping service [Tomcat]
2020-10-13 12:32:50.410 INFO --- [ main] ConditionEvaluationReportLoggingListener :
What's the simplest way I can get this working again?
Fixed by replacing JdbcOperationsSessionRepository with JdbcIndexedSessionRepository

There was an unexpected error (type=Not Found, status=404). - Spring Java

I have this service in github https://github.com/felloz/personsService The application compile with no errors but when im trying to access the route http://localhost:8080/demo/persons Im getting this error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon May 11 14:55:30 VET 2020
There was an unexpected error (type=Not Found, status=404).
No message available
The compile message:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.7.RELEASE)
2020-05-11 15:05:55.576 INFO 10476 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on lserrano-PC with PID 10476 (C:\JAVA\Eclipse Projects\demo\target\classes started by lserrano in C:\JAVA\Eclipse Projects\demo)
2020-05-11 15:05:55.592 INFO 10476 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-05-11 15:05:58.173 INFO 10476 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-11 15:05:58.198 INFO 10476 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-11 15:05:58.198 INFO 10476 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-05-11 15:05:58.386 INFO 10476 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-05-11 15:05:58.387 INFO 10476 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2685 ms
2020-05-11 15:05:58.857 INFO 10476 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-05-11 15:05:59.362 INFO 10476 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-11 15:05:59.377 INFO 10476 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 4.578 seconds (JVM running for 5.433)
2020-05-11 15:06:14.712 INFO 10476 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-11 15:06:14.712 INFO 10476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-05-11 15:06:14.727 INFO 10476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 15 ms
You can check the code in the github repo, Im new with Sprong any help will be appreciated, im using NetBeans as IDE.
After adding server.servlet.context-path=/demo
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.7.RELEASE)
2020-05-11 17:26:54.336 INFO 9144 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on lserrano-PC with PID 9144 (C:\JAVA\Eclipse Projects\demo\target\classes started by lserrano in C:\JAVA\Eclipse Projects\demo)
2020-05-11 17:26:54.336 INFO 9144 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2020-05-11 17:26:56.926 INFO 9144 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-05-11 17:26:56.957 INFO 9144 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-11 17:26:56.957 INFO 9144 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-05-11 17:26:57.160 INFO 9144 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring embedded WebApplicationContext
2020-05-11 17:26:57.160 INFO 9144 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2715 ms
2020-05-11 17:26:57.612 INFO 9144 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-05-11 17:26:58.111 INFO 9144 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/demo'
2020-05-11 17:26:58.111 INFO 9144 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 4.591 seconds (JVM running for 5.464)
2020-05-11 17:27:14.361 INFO 9144 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/demo] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-11 17:27:14.362 INFO 9144 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-05-11 17:27:14.379 INFO 9144 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
you have #RequestMapping({"/persons"}) in your controller, so try this link http://localhost:8080/persons without demo
after spring 2 , context path configuration has been changed ;
server.servlet.context-path=/demo
And then try http://localhost:8080/demo/persons
If you don't want to use Jpa, following changes would be needed :
Context Path should be set as:
server.servlet.context-path=/demo
DemoApplication Class (correcting base package):
#SpringBootApplication
#ComponentScan(basePackages = {"com.example"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
You need to provide the implementation for your Repositoory:
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;
#Repository
public class PersonRepository{
public List<Person> findAll(){
List<Person> list = new ArrayList<>();
Person person = new Person();
person.setId(10001);
person.setName("Name");
person.setLastname("LastName");
list.add(person);
return list;
}
}
To work with MySQL DB and SpringBoot , you can find many example on web. Please follow that. Example : SpringBootMySQLExample

Java: Eclipse console doesn't display Spring Controller information

When I run my spring boot application in eclipse 4.7.3 IDE, the console doesn't display the information about the rest controller. Nevertheless the controller is working when I test it with browser.
It is just a "Hello World" application:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class HelloWorldSpringBootApp {
public static void main(String[] args) {
SpringApplication.run(HelloWorldSpringBootApp.class, args);
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HelloWorldController {
#RequestMapping(value = "/")
public String helo() {
return "Hello World!";
}
}
Output:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)
2019-04-28 09:08:33.768 INFO 2208 --- [ main] c.infotech.app.HelloWorldSpringBootApp : Starting HelloWorldSpringBootApp on OfficeLaptop01 with PID 2208 (C:\Users\Admin\eclipse-workspace4.7.3a\Sprang\HelloWorldSpringBoot\target\classes started by Admin in C:\Users\Admin\eclipse-workspace4.7.3a\Sprang\HelloWorldSpringBoot)
2019-04-28 09:08:33.775 INFO 2208 --- [ main] c.infotech.app.HelloWorldSpringBootApp : No active profile set, falling back to default profiles: default
2019-04-28 09:08:34.852 INFO 2208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-04-28 09:08:34.873 INFO 2208 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-04-28 09:08:34.873 INFO 2208 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-28 09:08:34.978 INFO 2208 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-04-28 09:08:34.978 INFO 2208 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1157 ms
2019-04-28 09:08:35.183 INFO 2208 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-28 09:08:35.348 INFO 2208 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-04-28 09:08:35.351 INFO 2208 --- [ main] c.infotech.app.HelloWorldSpringBootApp : Started HelloWorldSpringBootApp in 1.904 seconds (JVM running for 2.561)
As above it just display until "Started HelloWorldSpringBootApp in 1.904 seconds" without the request mapping status & mapped URL path info. Why?
The log output was changed in Spring 2.1. That changelog states that
If you are trying to debug an application and you want to restore
Spring Boot 2.0 style logging you should add the following to your
application.properties
logging.level.web=debug
This change however does not output what I would expect given this RestController
#RestController
public class HelloWorldController {
#RequestMapping(value = "/hello")
public String hello() {
return "Hello World!";
}
}
If I set the log level to trace it gives the expected output (though not in the exact same way as Spring Boot 2.1).
10:53:26.427 [main] TRACE o.s.w.s.m.m.a.RequestMappingHandlerMapping -
c.i.e.d.r.HelloWorldController:
{ /hello}: hello()
The requirement for trace log level is most likely due to a change in Spring Framework 5.1 (see release note)

Spring Boot Application is starting up from a JUnit test but unable to access through URL

I have a JUnit Test that starts my spring boot appcliation (Application.java).
#RunWith(SpringRunner.class)
#SpringBootTest(classes = Application.class)
public class AppclaitionTest {
#Test
public void contextLoads(){
Application.main(new String[]{});
}
}
If I run the JUnit test, Application is successfully starting up, but not accessible through url
Application Logs:
2017-06-16 12:18:07.918 INFO 207028 --- [ main] com.chandu.test.AppclaitionTest : Started AppclaitionTest in 1.927 seconds (JVM running for 2.458)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)
2017-06-16 12:18:08.012 INFO 207028 --- [ main] com.test.app.Application : Starting Application on IVL-WS39 with PID 207028 (started by Bhanuchandar.Challa in D:\Jars\SpringJDBCMySQL)
2017-06-16 12:18:08.012 INFO 207028 --- [ main] com.test.app.Application : No active profile set, falling back to default profiles: default
2017-06-16 12:18:08.012 INFO 207028 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#3f1ddac2: startup date [Fri Jun 16 12:18:08 IST 2017]; root of context hierarchy
2017-06-16 12:18:08.402 INFO 207028 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-16 12:18:08.417 INFO 207028 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-06-16 12:18:08.417 INFO 207028 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14
2017-06-16 12:18:08.526 INFO 207028 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-06-16 12:18:08.526 INFO 207028 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 514 ms
2017-06-16 12:18:08.636 INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-16 12:18:08.636 INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-16 12:18:08.636 INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-16 12:18:08.636 INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-16 12:18:08.636 INFO 207028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-16 12:18:08.933 INFO 207028 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#3f1ddac2: startup date [Fri Jun 16 12:18:08 IST 2017]; root of context hierarchy
2017-06-16 12:18:08.933 INFO 207028 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/getRowCount]}" onto public java.lang.Integer com.test.app.controller.TestController.getRowCount(java.lang.String)
2017-06-16 12:18:08.949 INFO 207028 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/process]}" onto public java.lang.String com.test.app.controller.TestController.processRequest()
2017-06-16 12:18:08.949 INFO 207028 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-06-16 12:18:08.949 INFO 207028 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-06-16 12:18:08.964 INFO 207028 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-16 12:18:08.964 INFO 207028 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-16 12:18:08.980 INFO 207028 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-06-16 12:18:09.105 INFO 207028 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-06-16 12:18:09.151 INFO 207028 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-06-16 12:18:09.151 INFO 207028 --- [ main] com.test.app.Application : Started Application in 1.186 seconds (JVM running for 3.689)
When I tried to access the application through url 'http://localhost:8080/process', It says Site can't be reached.
Why would you do such thing? This should be a unit test and the flow of the test should be the following:
Start the application -> Call your controller endpoint -> assert that a specific text/element on that page is present -> Shut down the application.
Start the application : #RunWith(SpringRunner.class) doing this for you, no need to start is manually.
Shut down the application : At the end of your test class Spring boot does this for you (That's why you cant access your app in the browser)
For further help please see my answer here: How to test (rest) enpoints
By default, in #SpringBootTest annotation the field webEnvironment is set to WebEnvironment.MOCK
Therefore, Tomcat does not start.
You can set this field to WebEnvironment.DEFINED_PORT/RANDOM_PORT.
After this, Tomcat will be running.
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class MyTestClass {
...
...
...
}

Categories

Resources