Using Spring Actuator with Spring CommandLineRunner application - spring-boot

I am developing a TCP socket server application in java using Spring Boot. Eventhough, I am not using the spring-boot-starter-web dependency, I still would like to benefit from the actuator endpoint to monitor the application using external monitoring tool like prometheus.
A minimal equivalent application would be:
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.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DemoApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
SpringBootConsoleApplication.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
#Component
public class SpringBootConsoleApplication
implements CommandLineRunner {
private static Logger LOG = LoggerFactory
.getLogger(SpringBootConsoleApplication.class);
#Override
public void run(String... args) throws InterruptedException {
LOG.info("EXECUTING : command line runner");
int i = 0;
while(true) {
LOG.info("iteration: {}", i++);
Thread.sleep(1000);
}
}
}
application.yml
spring:
jmx:
enabled: true
management:
endpoints:
jmx:
unique-names: true
exposure:
include: '*'
web:
exposure:
include: '*'
health:
show-details: always
Using jconsole, I can get the actuator data via JMX, but how to get them from the actuator rest endpoint?
Are there some missing (or unnecessary) config in the application.yml or missing dependencies?
I have read the other posts:
how could access actuator endpoints in non-web application
Spring boot actuator for commandline runner application
without managing to make the rest actuator working on my application.

As you have a command line application there is no web server included and therefore also no Actuator REST endpoints.

Related

Application execution failed and not getting expected outcome

I am developing a simple spring boot application. I am creating a LoginController class and trying to access the API which is present in the LoginController class. I am able to execute the program, but not getting the expected output while I am trying to access the API.
Login Controller
package com.in28minutes.springboot.web.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class LoginController {
#GetMapping("/login")
public String loginMessage() {
return "Welcome to Springboot Application";
}
}
Bootstraping Application
package com.in28minutes.springboot.web.springbootwebexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootWebExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebExampleApplication.class, args);
}
}
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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.in28minutes.springboot.web</groupId>
<artifactId>spring-boot-web-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-web-example</name>
<description>Demo project for Spring Boot</description>
<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.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
I run the project several times but don't get the output as expected. Can anyone help me out?
#SpringBootApplication annotation scans the current package and the sub-packages to look for #Component. In your case, LoginController is not in the package, not a sub-package of SpringBootWebExampleApplication.
Either move com.in28minutes.springboot.web.controller in com.in28minutes.springboot.web.springbootwebexample.controller
Or, use #SpringBootApplication(scanBasePackages = "your.package") on your SpringBootWebExampleApplication
package com.in28minutes.springboot.web.springbootwebexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = "com.in28minutes.springboot")
public class SpringBootWebExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebExampleApplication.class, args);
}
}
package com.in28minutes.springboot.web.springbootwebexample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#ComponentScan("com.in28minutes.springboot")
public class SpringBootWebExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebExampleApplication.class, args);
}
}

How to access application.yml properties in Spring 1.5.9

I'm spinning my wheels on this.
Here is a simple Spring Boot app. I'm trying to use a yaml properties file, but I can't seem to get it to find the properties file or let me access the values in it. I've tried many variations from advice I've searched for here and elsewhere. Nothing is working.
(For reasons I can't go into, I have to use an older version of Spring. (1.5.9))
In IntelliJ I have the Lombok plugin installed, annotations enabled and the language is set to Java 1.8.
Here is the error I'm getting currently:
[ERROR] demo/src/main/java/com/example/demo/Foo.java:[9,10] cannot find symbol
[ERROR] symbol: method value()
[ERROR] location: #interface lombok.Value
[ERROR] demo/src/main/java/com/example/demo/Foo.java:[9,3] annotation type not applicable to this kind of declaration
[ERROR] demo/src/main/java/com/example/demo/Foo.java:[13,5] cannot find symbol
[ERROR] symbol: variable log
[ERROR] location: class com.example.demo.Foo
Here is the folder structure:
Here is the application.yml file:
project:
thing:
path: resources/foo_files
Here is the main class:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
Foo foo = new Foo();
SpringApplication.run(DemoApplication.class, args);
}
}
Here is class Foo:
package com.example.demo;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
#Slf4j
class Foo {
#Value("${project.thing.path}")
private String projectThingPath;
public Foo() {
log.info("path is: " + projectThingPath);
}
}
Here is the 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.21.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
For getting value from application.yml you should be using #Value from the package org.springframework.beans.factory.annotation.Value but you are using lombok's #Value
package com.example.demo;
import org.springframework.beans.factory.annotation.Value; // Changed
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
#Slf4j
#Component // EDIT : Added
class Foo {
#Value("${project.thing.path}")
private String projectThingPath;
public Foo() {
log.info("path is: " + projectThingPath);
}
}
EDIT
So another important point is that, a class should be annotated with stereotype annotation ( To tell Spring to configure the data).
You have compile error because you are using #Slf4j (lombok annotation) but you have not Slf4j lib in your dependencies
In this case you can use #Log or add Slf4j to your dependencies

Spring boot with Java 7 in Weblogic

I have a requirement to deploy the spring boot application in weblogic server 11g. The weblogic server supports only Java 7. Please assist me with the correct spring boot version and I get the following error if I use spring boot version 1.5.6.RELEASE.
On hover the following message gets displayed.
"Multiple markers at this line
- The type javax.servlet.ServletContext cannot be resolved. It is indirectly referenced from
required .class files
- The type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from
required .class files"
Application.java
package com.example.ap;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.WebApplicationInitializer;
#SpringBootApplication
#EnableAutoConfiguration
#ComponentScan({ "com.example.ap" })
public class Application extends SpringBootServletInitializer implements
WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
return builder.sources(Application.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.ap</groupId>
<artifactId>test</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath />
</parent>
<properties>
<java-version>1.7</java-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
ResourceController.java
package com.example.ap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/resource")
public class ResourceController {
#RequestMapping(method = RequestMethod.GET)
String readResource() {
return "hello!";
}
}
In src/main/webapp/WEB-INF folder, I have weblogic.xml and dispatcherServlet-servlet.xml
I have excluded the embedded tomcat, because I need to deploy it in weblogic. Please help me with finding the issue.
weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd">
<wls:context-root>sg-manutouch-lite-api</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
<wls:weblogic-version>10.3.6</wls:weblogic-version>
</wls:weblogic-web-app>
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
Weblogic 11g(10.3.4) will support servlet 2.5(max). If needed to create an application using servlet 2.5, then web.xml is mandatory. Spring boot's way of configuring the application by using SpringBootServletInitializer, WebApplicationInitializer will be supported from servlet 3.0 onwards only.
Thanks for the guidance M.Denium.

Spring Boot doesn't load application.yml config or?

I have a simple main app:
// Application.java
package com.my.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = "com.my")
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
Just a class:
// TestClass.java
package com.my.application;
public class TestClass
{
public TestClass()
{
}
}
With config:
//ApplicationConfiguration
package com.my.configuration;
import com.my.application.TestClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
#Configuration
public class ApplicationConfiguration
{
#Autowired
Environment env;
#Bean TestClass getTestClass()
{
System.out.println(env.getProperty("test"));
return new TestClass();
}
}
this is my pom file:
// 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.my</groupId>
<artifactId>test</artifactId>
<version>1.3.0</version>
<packaging>pom</packaging>
<name>test</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.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
the application.yml file is just:
// src/main/resources/application.yml
test: 123
Property "test" always = null.
What I was wrong?
Tried with #Value, #ConfigurationProperties,
#EnableConfigurationProperties,
#PropertySource("classpath:src/main/resources/application.yml") annotations,
without the snakeyaml library,
with another spring-boot versions,
but result always the same.
It has to do with Maven - application.properties is not part of the build when you're using <packaging>pom</packaging> in your pom file - hence when you start the Application, the file is not there to be read.
Remove <packaging>pom</packaging> from your pom and you should be good to go.
try adding dependency org.springframework.boot:spring-boot-configuration-processor
I also faced the same issue. Tried lot of things, but it nothing worked.
In the end, when I added below dependency in pom, my application is able to read application.yml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

Unable to run Spring Boot simple REST service

I'm trying to run a basic Spring Boot application created with the Spring Initializr. I see the application is pretty much the same of many examples available on the Web but I get a '404' page not found:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
#RestController
public class Controller {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String hello() {
return "it works!";
}
}
This is the main class:
package com.example.springtest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
The pom.xml includes just the web starter:
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<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.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 execution:
$ curl http://localhost:8080
{"timestamp":"2018-12-11T09:18:59.236+0000","status":404,"error":"Not Found","message":"No message available","path":"/"}
Do I miss any starter to get it working?
Thanks
use #ComponentScan() annotation in the main method class and pass the path to the rest service component scan as a parameter.
because by default it only include the service which is in the package and sub package of the main class . so for adding other path service you have to pass the path in component scan annotation.

Resources