Spring Boot MVC : URL not published but shown in the boot logs - java

I'm just starting a very simple Spring MVC with Spring Boot (and Groovy), and I can't get my controller to be routed correctly, for example :
package net.kedare
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
#SpringBootApplication
class SpringLedControllerApplication {
static void main(String[] args) {
SpringApplication.run SpringLedControllerApplication, args
}
}
#Controller
#RequestMapping("/")
class SpringLedController {
#RequestMapping(method=RequestMethod.GET)
public String index() {
return "Hello"
}
}
It appears in the boot logs :
2016-01-23 14:09:28.588 INFO 57995 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[GET]}" onto public java.lang.String net.kedare.SpringLedController.index()
But when I go to the URL : "http://localhost:8080/", I get this :
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jan 23 14:09:58 CET 2016
There was an unexpected error (type=Not Found, status=404).
No message available
Looks like it redirects to /error, but I don't know why..

Add this dependency in your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Related

Spring Boot Controller not mapping (Whitelabel Error Page)

I have a Spring REST project that is redirecting all requests to error page, even if they are mapped in the controller.
I reduced the code to the smallest possible version that produces the error:
Here is the project structure:
Here is the Application class (The imports are removed to make the thread easier to read):
package com.example.demo;
#Controller
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#GetMapping("/greeting")
#ResponseBody
public String greeting() {
return "greeting";
}
}
Originally I hade a sperate controller from the App class, but moved the controller code to the app class to make sure that this is not a project structure problem
Here is the controller code (Tried with and without it, and received the same error):
#Controller
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#GetMapping("/hello")
#ResponseBody
public String greeting() {
return "greeting";
}
}
(Both http://localhost:8080/greeting as well as well http://localhost:8080/hello return the same error page)
Dependencies and plugins from the pom file:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>
(Tried with and without tomcat as dependency and nothing changed)
And lastly here is the error message I receive in the browser when I visit the links (http://localhost:8080/greeting and http://localhost:8080/hello):
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Nov 27 00:16:08 CET 2022
There was an unexpected error (type=Not Found, status=404).
Edit:
After setting debug to true in project.properties, here is the error message I see in console (Worth mentioning that the project ran with no issues when I tried it on another system (Same OS)):
GET "/greeting", parameters={}
Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
Resource not found
Completed 404 NOT_FOUND
"ERROR" dispatch for GET "/error", parameters={}
(Timestamps are removed to make reading easier)
Did you tried making call to the endpoint via postman ? If so, can you try again after removing #ResponseBody annotation.
Instead of #Controller use #RestController

spring boot - #PostConstruct not called on #Component

I am new to spring, and I have created a new spring boot project using https://start.spring.io/ with no further dependencies, unzipped the zip file and opened the project in IntelliJ IDEA. I have not done any further configurations. I am now trying to setup a bean with a #PostConstruct method - however, the method is never invoked by spring.
These are my classes:
SpringTestApplication.java
package com.habichty.test.testspring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
#SpringBootApplication
public class SpringTestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringTestApplication.class, args);
context.getBean(TestBean.class).testMethod();
}
}
TestBean.java
package com.habichty.test.testspring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
#Component
public class TestBean {
private final Logger log = LoggerFactory.getLogger(this.getClass());
private int a = 1;
public TestBean()
{
log.debug("Constructor of TestBean called.");
}
#PostConstruct
public void init()
{
log.debug("init()-Method of TestBean called.");
a = 2;
}
public void testMethod()
{
log.debug("Test Method of TestBean called. a=" + a);
}
}
When I start the application, this is my output:
:: Spring Boot :: (v1.5.9.RELEASE)
2018-01-22 13:15:57.960 INFO 12035 --- [ main] c.h.t.testspring.SpringTestApplication : Starting SpringTestApplication on pbtp with PID 12035 (/home/pat/prj/testspring/testspring/target/classes started by pat in /home/pat/prj/testspring/testspring)
2018-01-22 13:15:57.962 DEBUG 12035 --- [ main] c.h.t.testspring.SpringTestApplication : Running with Spring Boot v1.5.9.RELEASE, Spring v4.3.13.RELEASE
2018-01-22 13:15:57.962 INFO 12035 --- [ main] c.h.t.testspring.SpringTestApplication : No active profile set, falling back to default profiles: default
2018-01-22 13:15:58.018 INFO 12035 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext#2931522b: startup date [Mon Jan 22 13:15:58 CET 2018]; root of context hierarchy
2018-01-22 13:15:58.510 DEBUG 12035 --- [ main] com.habichty.test.testspring.TestBean : Constructor of TestBean called.
2018-01-22 13:15:58.793 INFO 12035 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-01-22 13:15:58.822 INFO 12035 --- [ main] c.h.t.testspring.SpringTestApplication : Started SpringTestApplication in 1.073 seconds (JVM running for 2.025)
2018-01-22 13:15:58.822 DEBUG 12035 --- [ main] com.habichty.test.testspring.TestBean : Test Method of TestBean called. a=1
2018-01-22 13:15:58.826 INFO 12035 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext#2931522b: startup date [Mon Jan 22 13:15:58 CET 2018]; root of context hierarchy
2018-01-22 13:15:58.828 INFO 12035 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
As you can see, spring initializes the TestBean and also executes the testMethod() - but the init()-Method, annotated with #PostConstruct, is not invoked.
What am I doing wrong?
Any help is very appreciated.
UPDATE 1
In my application.properties, I have configured:
logging.level.com = DEBUG
Changing this to logging.level.root = DEBUG results in a massively bigger log. However, it still does not contain the debug message of my init() method.
UPDATE 2 Added package and import statements.
UPDATE 3 To further clarify that this is not a logging issue, I have added an new int to the code that should be altered by the init()-Method. As far as I understood the concept of the #PostConstruct annotation, it should be executed prior to any other method execution. As a consequence, the output of testMethod() should now contain a=2. In the updated output, you may see that this is not the case.
UPDATE 4 This is my POM
<?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.habichty.test.testspring</groupId>
<artifactId>springTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springTest</name>
<description>springTest</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The output of java -version:
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
Due to the new module system in Java9 SpringBoot-1.5.9 fails to process #PostConstruct as the annotation class is not on the classpath. The problem (or similar) is described here and here. There are a few ways to resolve it:
run the application with Java8, or, if still on Java9:
add javax.annotation:javax.annotation-api dependency to the POM, or
upgrade to a newer Spring-Boot of version 2.0.0+ (which is still PRERELEASE as of this writing) which incorporates that dependency;
I know this Problem is already solved, but as it is the first Stackoverflow Question that came up when I googled this problem I will leave this here:
I had the same issue and my error was that I had a typo in my package names. My class which was calling name had a different packagename than my SpringBootApplication class. So my main application was not finding my component.
So always check your package names if you have a similiar problems.
I guess you did not define something like
logging.level.root=debug
in your application.properties?
Without = no logs
With =
2018-01-22 12:34:06.117 DEBUG 8516 --- [main] com.example.demo.TestBean : Constructor of TestBean called.
...
2018-01-22 12:34:06.117 DEBUG 8516 --- [main] com.example.demo.TestBean : init()-Method of TestBean called.
...
2018-01-22 12:34:06.241 DEBUG 8516 --- [main] com.example.demo.TestBean : Test Method of TestBean called.
If you are using Java 9 or higher, then you will encounter an error when using #PostConstruct and #PreDestroy in your code.
Eclipse is unable to import #PostConstruct or #PreDestroy
When using Java 9 and higher, javax.annotation has been removed from its default classpath. That's why Eclipse can't find it.
Solution
Download the javax.annotation-api-1.3.2.jar from
https://search.maven.org/remotecontent?filepath=javax/annotation/javax.annotation-api/1.3.2/javax.annotation-api-1.3.2.jar
Copy the JAR file to the lib folder of your project
Use the following steps to add it to your Java Build Path.
Right-click your project, select Properties
On the left-hand side, click Java Build Path
In the top-center of dialog, click Libraries
Click Classpath and then Click Add JARs ...
Navigate to the JAR file /lib/javax.annotation-api-1.3.2.jar
Click OK then click Apply and Close
Eclipse will perform a rebuild of your project and it will resolve the related build errors.

Spring boot war not working on EAP 6

I have created a small REST-based application using spring boot. The application is deployed on EAP 6(JBoss) as a war package.
As EAP 6 is based on Java 1.7 I have configured that in my maven pom to compile and use Java 1.7 version.
When I am deploying the application I can see in the server logs that the controller is getting registered but when I am hitting it I am getting 404. Also I JBoss is not picking up my context root configuration but taking the application name as the context root. I am tested all the possible endpoints but everything is giving 404.
Can someone suggest me something which can help me to proceed forward?
POM file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
....
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Application configuration
package com.org.orderhistory.v2.orderhistory.v2;
import ...
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.org.orderhistory.v2.orderhistory.v2.controllers;
import ...
#RestController
#RequestMapping(value="/myorder/weborders")
public class WebOrderControllers {
#RequestMapping(value="/{webUserId}",method = RequestMethod.GET, produces = "application/json")
public List<WebOrder> getWebOrdersForUser(#PathVariable Long webUserId) {
JBoss Logs
2017-10-09 02:24:29,744 [ServerService Thread Pool -- 594] INFO [org.springframework.boot.web.servlet.FilterRegistrationBean] Mapping filter: 'requestContextFilter' to: [/*]
2017-10-09 02:24:30,368 [ServerService Thread Pool -- 594] INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter] Looking for #ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#5a46b924: startup date [Mon Oct 09 02:24:27 EDT 2017]; root of context hierarchy
2017-10-09 02:24:30,451 [ServerService Thread Pool -- 594] INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] Mapped "{[/org/weborders/{webUserId}],methods=[GET],produces=[application/json]}" onto public java.util.List<com.org.www.order.model.WebOrder> com.org.orderhistory.v2.orderhistory.v2.controllers.WebOrderControllers.getWebOrdersForUser(java.lang.Long)
Exact same issue here. Try this:
1. In pom.xml:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
2. Add a class implements WebApplicationInitializer:
#Configuration
public class WebApplicationInitializerImpl implements WebApplicationInitializer{
#Override
public void onStartup(ServletContext container) throws ServletException {
WebApplicationContext context = getContext();
Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
registration.setLoadOnStartup(1);
registration.addMapping("/*");
}
private WebApplicationContext getContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(ApplicationMain.class.getName());
return context;
}
}
3. Remember to extend SpringBootServletInitializer by your application:
#SpringBootApplication
public class ApplicationMain extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ApplicationMain.class);
}
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}
If it works, then the problem is related to dispatcherServlet.
When using Spring boot, a dispatcherServlet should have been configured automatically. (That is a part of auto Configuration)
And this situation seems to be the issue of jboss, according to Redhat's explanation:
Because JBoss enforces the servlet specification's requirements about not registering conflicting mappings even for it's own server-added DefaultServlet.
To check if your dispatcherServlet is registered as expected, try this:
[standalone#localhost:9999 /] /deployment=spring-boot-application.war/subsystem=web:read-resource(recursive=true)
Not working:
{
"outcome" => "success",
"result" => {
"context-root" => "/spring-boot-application",
"servlet" => undefined,
"virtual-host" => "default-host"
}
}
Working:
{
"outcome" => "success",
"result" => {
"context-root" => "/spring-boot-application",
"virtual-host" => "default-host",
"servlet" => {"appServlet" => {
"servlet-class" => "org.springframework.web.servlet.DispatcherServlet",
"servlet-name" => "appServlet"
}}
}
}
Hope it solves your problem.
Reference:
https://access.redhat.com/solutions/1211203
https://blog.csdn.net/u011160656/article/details/78809239

Spring Boot | localhost: 8080 404 error page displayed

I created a Spring Boot Maven project, however my RequestMapping, as well as localhost:8080 return a 404 error page. I think the issue is with how my packages are setup, but I've tried solutions in multiple questions, and I still cant get around the error page. Could you guys point me in the right direction as to how to resolve this issue? Perhaps I need to add the Component annotation above my Main class? But I've tried this solution, and the error still persists.
Here is my package structure:
/src/main/java
ControllerLayer
UsersController.java
DataAccessLayer
UsersDAL.java
ServiceLayer
UsersService.java
Main
Main.java
Main.java:
#SpringBootApplication(scanBasePackages = {
"/src/main/java/ControllerLayer", "/src/main/java/DataAccessLayer",
"/src/main/java/ServiceLayer" })
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
UsersController.java:
import Entities.Users;
import ServiceLayer.UsersService;
#RestController
#RequestMapping("/users")
public class UsersController {
#Autowired
private UsersService usersService;
#RequestMapping(value =
"/create/{userId}/{userPassword}/{userAge}/{userEmail}"
+ "/{userFirstName}/{userlastName}", method =
RequestMethod.POST)
public void createUser(#PathVariable("userId")String userId,
#PathVariable("userPassword")String userPassword,
#PathVariable("userAge")int userAge,
#PathVariable("userEmail")String userEmail,
#PathVariable("userFirstName")String userFirstName,
#PathVariable("userLastName")String userLastName) {
usersService.createUser(new Users(userId, userPassword,
userAge, userEmail, userFirstName, userLastName));
}
}
UserService.java
import DataAccessLayer.UsersDAL;
import Entities.Users;
#Service
public class UsersService {
#Autowired
private UsersDAL usersDAL;
public void createUser(Users user) {
usersDAL.createUser(user);
}
}
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>xProjectAlpha</groupId>
<artifactId>org.htech.xProjectAlpha</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<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>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.9.Final</version><!--$NO-MVN-MAN-VER$-->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
When a request is sent, then a response shall be returned. In your case, you didn't send any content with the response and that's why you get 404 error (page not found).
In main.java, try:
#SpringBootApplication(scanBasePackages = {
"ControllerLayer", "DataAccessLayer",
"ServiceLayer" })
Your package names shouldn't include the root path in the project.
It is advisable to have spring boot Application class in root package and have all other classes in package structure below that package .You don't have to worry about component scan as an example
package com.igt.customer;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
#Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
Controller class
package com.igt.customer.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class EmployeeController {
#RequestMapping("/employee")
public String employee() {
return "Greetings from Sam!";
}
}
running the application (go to the directory of your application on cmd )
E:\MongoDb\New folder\customer>mvn install -U -e
you should see this in the end if its fine
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.686 s
[INFO] Finished at: 2017-08-16T16:39:57+05:30
[INFO] Final Memory: 21M/219M
[INFO] ------------------------------------------------------------------------
run the jar file
E:\MongoDb\New folder\customer\target>java -jar customer-0.0.1-SNAPSHOT.jar
accessing the application
http://localhost:8080/employee
Notice application name is not required in URL
P.S i have written extra detail here as i have experienced if you are new to spring boot building and running the application is a challenge , in my application i had created a rest controller in the same package as the Application class with RequestMapping "/" as i was getting 404 error , Please see the link below as a reference
spring boot application
This issue will be simply solved if you remove the main package of the main.java class.
The new structure will be:
/src/main/java
Main.java
ControllerLayer
UsersController.java
DataAccessLayer
UsersDAL.java
ServiceLayer
UsersService.java
In my Spring boot application, there is no need to scan the base packages manually because all the configurations are embedded in a single annotation #SpringBootApplication. Please refer to this link.
I don't understand how the base packages are initially configured. Can someone please explain this?
For example, if your base package looks like:
com.example.myapp.SpringApplication
... it means your application takes base packages as com.example.myapp. So if you can create all Controllers, Service, Repository under com.example.myapp in the sense it will load your Controllers, Service, Repository easily or else it can't able to load. This is because springbootapplication intially sets the base packages and loads whatever java classes are inside the base package. So because of this you get a 404 error in the browser as well as in postman. So try to match with base package.

Spring Boot web application doesn't serve standard endpoints

I have a problem with a Spring Boot Application that is supposed to run from command line and at the same time serve some metrics from the standard /metrics endpoint. When I just created the application all the metrics were served correctly, but at some point I seem to have "broken" something and my application stopped serving from default endpoints. I can't just revert to the initial state because there's a lot of code already and I don't want to lose version control history. Maybe someone could point at what I am doing wrong?
I don't override dispatcher servlet and don't add any custom filters.
Spring Boot version 1.3.7.
Error when accessing /metrics or any other default endpoint:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing
this as a fallback.
Mon Oct 03 17:53:12 PDT 2016 There was an unexpected error (type=Not
Found, status=404). No message available
Application file:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Runner.class, args);
}
}
Main runner file:
#EnableConfigurationProperties(ApplicationProperties.class)
#SpringBootApplication
public class Runner implements CommandLineRunner {
#Override
public void run(String... strings) throws Exception {
// shortened ...
}
}
POM file fragment:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.7.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Debug output shows that the filters are created:
2016-10-03 17:26:14.461 DEBUG 85880 --- [ost-startStop-1] o.s.b.c.e.ServletContextInitializerBeans : Added existing Servlet initializer bean 'dispatcherServletRegistration'; order=2147483647, resource=class path resource [org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class]
2016-10-03 17:26:14.720 DEBUG 85880 --- [ost-startStop-1] o.s.b.c.e.ServletContextInitializerBeans : Created Filter initializer for bean 'metricFilter'; order=-2147483648, resource=class path resource [org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.class]
I created a sample clean application to compare and there are lines in output that I don't have in my app:
2016-10-03 18:06:48.075 DEBUG 86858 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : 2 request handler methods found on class org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint: {public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)={[/{name:.*}],methods=[GET],produces=[application/json]}, public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()={[],methods=[GET],produces=[application/json]}}
2016-10-03 18:06:48.076 INFO 86858 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2016-10-03 18:06:48.076 INFO 86858 --- [ main] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
So, the filters/servlet seem to be created but not mapped in my app.
What am I possibly missing here?
OK I was stupid. Could say it twice.
So, after all, the problem was my bad memory. I actually was overriding application properties like this:
management.contextPath=/services/admin
Guess what, my /metrics was there all the time. It was just under /services/admin/metrics, which I completely forgot to have overridden. Duh.

Categories

Resources