I have an existing war project created using spring boot. How to package it within an EAR which has an EJB module?
Is there any way to move the model and dao packages to EJB module and injecting it with WAR module?
You need a parent project that includes a war project, which will be your spring boot project, and an ear project just for making your ear.
Parent will need to have the spring boot as its parent :
<?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.4.3.RELEASE</version>
</parent>
<groupId>com.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<myproject.version>1.0-SNAPSHOT</myproject.version>
</properties>
<name>ear-example</name>
<modules>
<module>example-ear</module>
<module>example-war</module>
</modules>
</project>
Your ear project is:
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>example-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>example-war</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<modules>
<webModule>
<groupId>com.greg</groupId>
<artifactId>example-war</artifactId>
<contextRoot>/appname</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
You have to use the dependency management system.
It allows you to set the parent of a Spring Boot WAR module project that is different from spring-boot-starter-parent. It would make it possible to include the WAR project into EAR in the same way just like any other.
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
... then you would use starter dependencies in the usual way:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
Shared dependencies may be specified by the root level pom while the individual ones might be a module bounded.
Using Spring Boot without the parent POM
I have created a multi module gradle project including a spring RESTFul web services -
EAR application name is - bluestone
bluestone/settings.gradle -
rootProject.name = 'bluestone'
include ':bluestone-web'
include ':bluestone-core'
include ':bluestone-rest'
project (':bluestone-web').projectDir = new File('bluestone-web')
project (':bluestone-core').projectDir = new File('bluestone-core')
project (':bluestone-rest').projectDir = new File('bluestone-rest')
bluestone-rest project structure is -
bluestone-rest/build.gradle
plugins {
id 'war'
}
group 'com.bluestone.smart.rest'
version '1.0-SNAPSHOT'
dependencies {
compile library.spring_context
compile library.spring_web
compile library.spring_beans
compile library.spring_mvc
providedCompile library.servlet_api
testCompile library.junit
}
all the dependencies are imported from common libraries.gradle. common libraries.gradle is user ear bluestone/libraries.gradle
/* ============================================================================
Library definitions for project 'Bluestone'
============================================================================
Define here library dependencies and use them inside sub-modules build.gradle.
Included from: "${rootProject.projectDir}/build.gradle"
============================================================================
*/
ext {
library = [
/* testing */
junit: "junit:junit:4.12",
log4j: "log4j:log4j:1.2.17",
/* Spring libraries*/
spring_context: "org.springframework:spring-context:${spring_lib_version}",
spring_aop: "org.springframework:spring-aop:${spring_lib_version}",
spring_beans: "org.springframework:spring-beans:${spring_lib_version}",
spring_orm: "org.springframework:spring-orm:${spring_lib_version}",
spring_web: "org.springframework:spring-web:${spring_lib_version}",
spring_mvc: "org.springframework:spring-webmvc:${spring_lib_version}",
servlet_api: "javax.servlet:javax.servlet-api:4.0.1"
]
}
Within bluestone-rest, i have created three basic file to test a sample rest message -
spring Configuration named - BlueRestConfiguration.java
package com.bluestone.smart.rest.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"com.bluestone.smart.rest.resources", "com.bluestone.smart.rest.controller"})
public class BlueRestConfiguration {
}
Initialization file - named is - RestInit.java
package com.bluestone.smart.rest.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.ServletContext;
public class RestInit extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* Specify {#code #Configuration} and/or {#code #Component} classes for the
* {#linkplain #createRootApplicationContext() root application context}.
*
* #return the configuration for the root application context, or {#code null}
* if creation and registration of a root context is not desired
*/
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {BlueRestConfiguration.class};
}
/**
* Specify {#code #Configuration} and/or {#code #Component} classes for the
* {#linkplain #createServletApplicationContext() Servlet application context}.
*
* #return the configuration for the Servlet application context, or
* {#code null} if all configuration is specified through root config classes.
*/
#Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
/**
* Specify the servlet mapping(s) for the {#code DispatcherServlet} —
* for example {#code "/"}, {#code "/app"}, etc.
*
* #see #registerDispatcherServlet(ServletContext)
*/
#Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
A rest service API - named - GreetingsController.java
package com.bluestone.smart.rest.resources;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class GreetingsController {
#RequestMapping(path = "/greeting", method = RequestMethod.GET)
public String greetings(){
return "Welcome Spring Rest!";
}
}
finally build this EAR application using -
gradlew clean build
and deploy on WildFly application and then calling this service using postman -
Please let me know if any more details required. I shall push this code on git and will share the git link here.
Related
I am trying to use SpringBoot in javaFX application. I use javafx-weaver-spring-boot-starter, and it works great with java < 9, but there are some problems with java 9+ (modularity).
My project structure:
AppStarter code:
package test;
import javafx.application.Application;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class AppStarter {
public static void main(String[] args) {
Application.launch(JavaFxApplication.class, args);`
}
}
JavaFxApplication Code:
package test;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import net.rgielen.fxweaver.core.FxWeaver;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
public class JavaFxApplication extends Application {
private ConfigurableApplicationContext applicationContext;
#Override
public void init() {
String[] args = getParameters().getRaw().toArray(new String[0]);
this.applicationContext = new SpringApplicationBuilder()
.sources(AppStarter.class)
.run(args);
}
#Override
public void start(Stage stage) {
FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
Parent root = fxWeaver.loadView(MyController.class);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
#Override
public void stop() {
this.applicationContext.close();
Platform.exit();
}
}
MyController code:
package test;
import javafx.event.ActionEvent;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.stereotype.Component;
#Component
#FxmlView("hello-view.fxml")
public class MyController {
public void onHelloButtonClick(ActionEvent actionEvent) {
System.out.println("Hello World");
}
}
modules-info.java:
module test {
requires spring.boot.autoconfigure;
requires javafx.graphics;
requires spring.context;
requires net.rgielen.fxweaver.core;
requires spring.boot;
requires javafx.weaver.spring.boot.starter;
requires net.rgielen.fxweaver.spring.boot.autoconfigure;
requires net.rgielen.fxweaver.spring;
exports test;
opens test;
}
pom.xml code:
<?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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo2</name>
<description>demo2</description>
<properties>
<java.version>17</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>
<dependency>
<groupId>net.rgielen</groupId>
<artifactId>javafx-weaver-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17-ea+11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17-ea+11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>1
When I try to run the application, I get the following exception:
....
Caused by: java.lang.IllegalAccessError: class test.AppStarter$$EnhancerBySpringCGLIB$$e15856c4 (in module test) cannot access class org.springframework.cglib.core.ReflectUtils (in unnamed module #0x4fb0f9) because module test does not read unnamed module #0x4fb0f9
at test/test.AppStarter$$EnhancerBySpringCGLIB$$e15856c4.CGLIB$STATICHOOK1(<generated>)
at test/test.AppStarter$$EnhancerBySpringCGLIB$$e15856c4.<clinit>(<generated>)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:593)
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363)
at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:585)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:110)
at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData$3.apply(AbstractClassGenerator.java:108)
at org.springframework.cglib.core.internal.LoadingCache$2.call(LoadingCache.java:54)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:61)
... 21 more
...
module test does not read unnamed module...
how can I fix it?
Spring 5 isn't friendly with the JavaFX Platform Module System
Spring won't really be built for modules until Spring 6/Springboot 3 is released.
Spring Framework 6 is indeed expected to introduce module-info descriptors for all core framework modules, with minimal required dependencies on core Java modules, enabling the JDK's jlink tool to build custom runtime images for Spring setups. There might be constraints with optional third-party libraries and certain configuration strategies, so it is still unclear how popular such explicit module usage will become among Spring users.
For Spring 5-based applications, the framework is not easily compatible with the Java Platform Module System.
So I don't recommend trying to build a modular JavaFX application relying on Spring until:
Spring 6/SpringBoot 3 is released,
AND
the dependent libraries you use are also made to be easily compatible with the Java module system.
For example, when all of your dependencies define a module-info.java.
How to make your project non-modular
However, you can still build a JavaFX 17 application today which relies on Spring 5 by making the JavaFX application non-modular and either providing the JavaFX modules in the base JDK you use or via command-line switches. For more information, see the answer to:
Intellij 2021.3.2, JavaFX Maven project not resolving dependencies correctly
Delete the module-info.java to make the project non-modular. Place only the JavaFX modules on the module path and add them by VM arguments, --module-path and --add-modules. See the openjfx.io for getting started documentation for more information on the settings.
For distribution of your non-modular application, see either:
JPackageScriptFX for Maven-based builds.
badass-runtime-plugin for Gradle-based builds.
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
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>
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.
Hello i've tried to create a simple controller and put it in the same package with the main class but it looks like it's not finding it.Basically, when i run the app it's not mapping the endpoint.
I just can't figure out what is wrong with it.
Here is the code:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestProject1Application {
public static void main(String[] args) {
SpringApplication.run(TestProject1Application.class, args);
}
}
package com.example.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class HomeController {
#RequestMapping(value="/hello", method = RequestMethod.GET)
public String sayHello() {
return "HELLO";
}
}
Project Structure and Console:
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tsv</groupId>
<artifactId>TestProject-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>TestProject-1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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-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>
Although in your log, I do not see 'ServletRegistrationBean' getting initialized, I find no issue with your configuration. Try execute the Spring boot app again & you should be able to invoke the HomeController via 'localhost:8080/hello'.