I start with learning Spring and I create basic project which creates database, insert values and next print it in web browser.
My problem is that when I have RestController in the same package like main class - its OK, but I want distribute it to other package and when I create new package, move the RestController it doesn't work. Let met explain:
My project looks like:
|-Springtestv_01
|-src/main/java
|--com.person <-- it's a main package
|-Main.java
|-Person.java
|-PersonLineRunner.java
|-PersonRepository.java
|-PersonController.java
|-com.controller <-- second package, I want put here PersonController.java
|-src/main/resources
|-data.sql
pom.xml
My controller looks:
#RestController
public class PersonController {
#Autowired PersonRepository personRepository;
#RequestMapping("/persons")
Collection<Person> persons(){
return this.personRepository.findAll();
}
}
When everything is in com.person package, I write in web brower http://localhost:8080/persons and it works correctly...
But I Want move PersonController.java to com.controller package, and when I moved it, my webbrowers calls me
There was an unexpected error (type=Not Found, status=404). No message
available
and I have no idea what I should do to repair it. Maybe I should change something in my pom.xml ??
My pom.xml looks like
<?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.person</groupId>
<artifactId>person</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringTest_v0_1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId><artifactId>h2</artifactId>
</dependency>
<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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>
spring-boot-starter-data-elasticsearch
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
It is generated automatically, I write only one dependency
<dependency>
<groupId>com.h2database</groupId><artifactId>h2</artifactId>
</dependency>
Use basePackages:
#ComponentScan(basePackages = { "com.person","com.controller"} )
I had the same problem the answers provided here worked for me but i had to add another spring annotation and it's more general in case dealing with a lot of repositories.
We have the following structure :
|-src/main/java
|--com.person
|--repositories
|--controllers
|--...
This then should be added in th main
#SpringBootApplication(scanBasePackages = {"com.person"})
#EnableMongoRepositories(basePackages = "com.person.repositories")
public class MainDemoApplication { //
}
Using a #SpringBootApplication annotation is equivalent to using #Configuration, #EnableAutoConfiguration and #ComponentScan.
From the documentation:
ComponentScan configures component scanning directives for use with
#Configuration classes. Provides support parallel with Spring XML's
element.
One of basePackageClasses(), basePackages() or its alias value() may
be specified to define specific packages to scan. If specific packages
are not defined scanning will occur from the package of the class with
this annotation.
You can either move it as you did or specify basePackages in #ComponentScan.
We can use #ComponentScan (basePackages = {"include your package name here"}).
Also if you have some common package naming format, then we can include just common part of package name with * like #ComponentScan(basePackages = { "com.*"}, so that all packages having that common package name will get scanned.
I had the same problem but suddenly found that my Application.java class (the class with main method and #SpringBootApplication annotation) located in different but parallel package with #Controller class.
The thing is that Application.java class should be in same or on top of all other packages, then we don't need any #ComponentScan and all beans will be scanned automatically. For example: if Application.java located in com.person and all application beans located in com.person, then it will work without #ComponentScan.
Assuming the main method is in the package called com.setech.app and a controller is in a package called com.setech.controller.
For spring-boot 1.3.x upwards try this by adding "scanBasePackages" like this.
#SpringBootApplication(scanBasePackages = { "com.setech"} )
public class ResttanslatorApplication {
public static void main(String[] args) {
SpringApplication.run(ResttanslatorApplication.class, args);
}
}
Credit goes to Kamil Wozniak from here.
Instead of using ComponentsScan, we can use #Import.
#Configuration
#Import({PersonController.class})
public class MainClassHome {
public static void main(String[] args) {
SpringApplication.run(MainClassHome.class, args);
}
}
For Maven Case: if you put the controller in another different sub-module(not same as main class), you should add dependency in pom.xml.
Moving the Springbootapplication(application.java) file to another package resolved the issue for me. Keep it separate from the controllers and repositories. You can use any number of packages and have multiple controllers.But use #ComponenScan(basePackages={" "," "}) and mention all the packages.
Also, you may face this issue Consider defining a bean of type.. click here!
You just only define a package name in #ComponentScan
like :
#SpringBootApplication
#ComponentScan({"com.project.pck_name"})
public class MainClassHome {
public static void main(String[] args) {
SpringApplication.run(MainClassHome.class, args);
}
}
and update project after that:- right click on Your project -> maven -> update project
As the one who has struggled with the issue, if you found provided solution didn't work.
Please check whether you put source files at the root level directly, for instance,
src\main\java\xxx.java
It has negative effect to project but I don't know the root cause. Anyway,
Please put source files to at least a self-created package like:
src\main\java\pack1\xxx.java
Try other settings again. It did solve my problem.
I find, that if you put the Controller classes in a separate jar, #Componentscan does not find the #RestControllers.
However, #SpringBootApplication(scanBasePackages =..
does that trick much better.
I used release train springboot spring-cloud-starter-parent, Greenwich.SR4
This resolves to spring boot 2.1.10.Release
So, in this release, #SpringBootApplication <> #Componentscan behaviour on that matter.
Related
I'm trying to use Azure Monitor OpenTelemetry Exporter client library for Java to export all traces/spans from Spring Cloud Sleuth to Azure Monitor. This integration seems to only work with the newest version of Spring Cloud Sleuth after the recent dependency version updates.
However instead of using azureMonitorExporter.export(spanData) manually, I would like to export all traces/spans automatically for the whole application by just adding a configuration for Azure exporter. This could then easily be added to a new project.
I don't have much experience using Spring/Sleuth/OpenTelemetry, but AzureMonitorExporter implements SpanExporter, so I thought one option could be to create a configuration class that contains the following.
#Bean
public SpanExporter exporter() {
return new AzureMonitorExporterBuilder()
.instrumentationKey("{KEY}")
.buildExporter();
};
And then it could be used in the main class using:
#Autowired
SpanExporter exporter;
However I'm not sure if this is the right way or how to continue from here to actually get the exporter to start exporting traces/spans automatically to Azure monitor.
My pom.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>trace-demo-5-updated</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>trace-demo-5-updated</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0-SNAPSHOT</spring-cloud.version>
</properties>
<dependencies>
<!-- Sleuth with Brave tracer implementation -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>opentelemetry-exporters-azuremonitor</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-otel</artifactId>
<version>3.0.0-M6</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
Update (2020-01-26):
With today's release of Spring Cloud Sleuth OTel (spring-cloud-sleuth-otel:1.0.0-M3) and The OTel upgrade in the Azure SDK, your original issue should disappear.
Original Answer
(TL;DR: last paragraph)
The Sleuth 3.0 Migration Guide can help you out a lot here. Sleuth supports two tracing systems Brave (default) and OpenTelemetry (incubator).
In order to use OpenTelemetry, you need to remove Brave (just exclude spring-cloud-sleuth-brave) and add OpenTelemetry (spring-cloud-sleuth-otel-autoconfigure with spring-cloud-sleuth-otel-dependencies). See the docs I linked.
After you do this, your SpanExporter should be used, here's a pom.xml I created based on yours. Btw, adding a sample project (e.g.: in a GitHub repo) could help a lot for everyone who is trying to help so that they don't need to create one themselves.
Unfortunately, this still won't work, you will still face with this exception at startup:
Caused by: java.lang.ClassNotFoundException: io.opentelemetry.common.AttributeValue
This is because opentelemetry-exporters-azuremonitor:1.0.0-beta.1 depends on OpenTelemetry 0.8.0 while the latest spring-cloud-sleuth-otel (1.0.0-M2, we released it today) is using the latest OpenTelemetry (0.13.1). Also, OpenTelemetry introduced breaking changes between minor versions, e.g.: the AttributeValue class was removed in OpenTelemetry 0.9.1.
Sleuth supports OpenTelemetry since Sleuth 3.0.0-M5, The OpenTelemetry version was 0.10 back then so there is no Sleuth version available that would support an OpenTelemetry version as old as opentelemetry-exporters-azuremonitor:1.0.0-beta.1 needs.
So the solution would be fixing your pom.xml and opentelemetry-exporters-azuremonitor supporting the latest OpenTelemetry.
Was testing a hardcoded endpoint for my Calorie management API. I hardcoded some users in my UserService.java and mapped them in my UserController to the /users url.
With Tomcat running on localhost:8080 I assumed that when I go to localhost:8080/users I would be able to see the users I added
Instead it takes me to a login page created by spring. Even though I can login with 'user' as the username and the generated password is in the build I do not want this to be implemented as I will be doing my own authentication in the future.
When going through the build file information the login page is connected to this information:
2020-04-23 11:22:38.314 INFO 1548 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#150d0d25, org.springframework.security.web.context.SecurityContextPersistenceFilter#7c82be70, org.springframework.security.web.header.HeaderWriterFilter#3258c818, org.springframework.security.web.csrf.CsrfFilter#18dd2f3, org.springframework.security.web.authentication.logout.LogoutFilter#336c3f7a, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#641198db, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter#7b7cd5a, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter#1c801106, org.springframework.security.web.authentication.www.BasicAuthenticationFilter#4b9896a8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#77927c43, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#6861d187, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#d43945e, org.springframework.security.web.session.SessionManagementFilter#19cc57e5, org.springframework.security.web.access.ExceptionTranslationFilter#7b984a77, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#2521604c]
I have no dependency that is related to security or authentication? is this coming from another dependency that has this information. Ive attached my pom.xl
<?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.3.0.M4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.MS3.bootcamp</groupId>
<artifactId>healthDiary</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>healthDiary</name>
<description>Bootcamp project for MS3</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.gurux</groupId>
<artifactId>gurux.dlms</artifactId>
<version>4.0.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
You can exclude the default spring security configuration by adding the annotation above your Application
#SpringBootApplication(exclude = { SecurityAutoConfiguration.class })
#ServletComponentScan
public class Application {
}
As you told on the question, seems like behavior as Spring Security is on the dependencies, so if you are dealing with Spring Security default login from http basic, the right solve would be configure it something like below:
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests().antMatchers("/**").permitAll()
.and();
}
}
The piece of code http.authorizeRequests().antMatchers("/**").permitAll() on this context, allow routes to no require authentication. The /** tells all possible routes from the context. By default the spring is all routes require authentication. So when you put Spring Security on you project with no security configuration, will force all user to login. Think that is for safety.
I made an project that was an simple example of RESTful API with spring boot, but I wasn't using 'Spring Boot Data Rest' that I published on Github, an the used class for this configuration was SecurityConfiguration.java, the full repository are on galeria-spring-boot.
Lots of more information about Spring Security on Security with Spring
I want to create a simple hello world app with SpringBoot where localhost:8080/welcome.html will show us Hello World.
I think I did all good but I can't see HelloWorld, just Whitelabel error page.
This is the link to my repo. If someone could check what is wrong I will be very happy!
https://github.com/BElluu/ElenXHello
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.springbelluu</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-helloworld</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</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>10</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>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
SpringbootHelloworldApplication.java
package com.springbelluu.springboothelloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringbootHelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHelloworldApplication.class, args);
}
}
TestController.java
package com.javainuse.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#RequestMapping("/welcome.html")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
}
application.properties
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
For starters if you want to get starter with Spring Boot I strongly suggest NOT to use JSP. There are quite some limitations when using JSP, one of them is it doesn't work with jar packaging. Secondly it is a dated technology and doesn't receive much attention/updates anymore apart from keeping if functional in newer JEE versions. It is better to use something like Thymeleaf.
Next you are using snapshots versions for a version of Spring Boot that already is at 2.1.3.RELEASE (at the moment of writing).
That being said change your pom.xml to the following (fix version, remove JSP stuff and replace with Thymeleaf).
<?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.springbelluu</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboot-helloworld</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>10</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-thymeleaf</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>
NOTE: Because you now use a final version you don't need all the repositories in your pom.xml anymore!.
Now delete your JSP and create a welcome.html in src/main/resources/templates/. (You can actually remove your webapp directory in full.
<html>
<body>
<h1>Welcome! Spring Boot for ElenX</h1>
</body>
</html>
The setup you now have is more modern and easier to work with then JSP.
In your application.properties remove the spring.mvc.view properties as Spring Boot will automatically configure Thymeleaf with correct settings.
#BElluu ... your main application and Controller class are in different packages so componentscan is not working ...just check package level
package com.springbelluu.springboothelloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringbootHelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHelloworldApplication.class, args);
}
}
package com.javainuse.controllers;
// use this package package com.springbelluu.springboothelloworld;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class TestController {
#RequestMapping("/welcome.html")
public ModelAndView firstPage() {
return new ModelAndView("welcome");
}
}
Go to below link by Spring Framework to create brand new Springboot Project:
https://start.spring.io/
or
1.Create a simple web application
Now you can create a web controller for a simple web application.
src/main/java/hello/HelloController.java
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
Create an Application class
Here you create an Application class with the components:
src/main/java/hello/Application.java
package hello;
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 Application {
public static void main(String[] args) {
SpringApplication.run(Application.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);
}
};
}
}
Here is SpringBoot Main class file Noting Changed Here.
-----------------------------------------
package com.Encee.SpringBoot_HelloWorld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
}
}
Here is controller package class
-----------------------------------------
package com.Encee.SpringBoot_HelloWorld.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class Controller {
#RequestMapping("/hello.html")
public String hello() {
return "Hello World";
}
}
-----------------------------------------
Now Main file run as Java Application will get your output.
SCREEN SHOT
Your issue has been fixed you can view the changes at my commit.
https://github.com/farooqrahu/ElenXHello/commits/master
Below is my pom.xml file. On the very first line, I am getting an error of
Error parsing lifecycle processing instructions.
I need help finding what caused the error.
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>First</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
This issue was driving me insane, I've finally managed to solve it by deleting the whole maven repository located in:
Windows: c:\Users\<username>\.m2\
After that I just updated the project (Alt+F5 in Eclipse). Issue gone!
Maybe a clash of different versions of Maven (I use several different versions of Eclipse + m2e plugins).
There is some dependency got corrupted into .m2 folder.
You need to delete that dependency from .m2 folder.
If you are not able to find which one is corrupted then delete all
the dependency which are declared into pom.xml file.
It is due to an older version of m2e-wtp integration plugin. To resolve the issue, in Eclipse(Neon) you can go to Help --> Install New Software.. --> Enter http://download.eclipse.org/m2e-wtp/milestones/neon/1.3/ in the Work with box and press enter. Tick all the checkboxes, install the plugins and restart the IDE. It should work. Similarly, for other versions, you might refer the following link https://www.eclipse.org/m2e-wtp/ and try the latest/previous builds depending on your Eclipse version.
Take a look at eclipse web
http://marketplace.eclipse.org/content/maven-integration-eclipse-wtp
The m2eclipse-wtp project has moved to the Eclipse Foundation. The m2eclipse-wtp plugin is now deprecated in favor of the newer m2e-wtp
Automatic installation from the marketplace has been suspended. m2eclipse-wtp 0.15.3 can be manually installed from http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/
You need to first uninstall all m2e-wtp plugins in Eclipse, then install them from jboss url
No need to clean .m2 folder, I fixed this issue by adding corresponding maven to eclipse
Inspite of adding the new path and making changes to the user settings the error "Error parsing lifecycle processing instructions" persists. I restarted the server. Is there any other change required?
This same problem happened to me.
I'm using the new Eclipse Neon. Maybe it's an issue related to the Maven version included in this new release.
I have solved it by using Gradle instead of Maven.
Note: you can try to use Maven with the previous version of Eclipse.
Try this, it might be helpful.
<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.demo</groupId>
<artifactId>test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java-version>1.8</java-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>HelloWorld</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>SpringWebExample</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
In my case the same issue was due to different version of maven. One which I installed manualy and other one which came Embedded with STS.
I resolved this issue using below steps:
1. Deleted .M2 folder (c:\Users\.m2)
Downloaded exact version of maven which is embedded with STS and configured same in env. variables and path variable.
STS Embedded maven- Image
Updated projects (Right click on project -> Maven -> Update Project...)
It took some time in update step as all dependencies downloaded again in .M2 folder. Finally issue is resolved.
I suffered the same problem in several projects of my workspace, the ones who belonged to the same parent. I have just one Maven version installed, and my local .m2 repository is working fine for a bunch of other maven projects in my wokspace.
In my case, the cause was a simple XML syntax error in the parent pom. I fixed it, refreshed the affected maven projects, and it went OK.
Before I made modifications to the .xml I made a copy in the same folder. After making my mods I began having issues.
Moving the backup out of the .m2 folder resolved the issue for me.
I'm a beginner in spring and I'm trying to develop a simple app however I'm struggling to find a tool which will help me find exactly what jars or what dependencies I should add to my project. I tried
http://search.maven.org/
It worked for some of the jars that I was looking for. It worked for the DriverManagerDataSource however it does not work for the
org.springframework.boot.SpringApplication;
To be more precise: Is there a tool that will tell me what jars I need for a specific import?
How can I know what jar I need in my build path for the following line of code to compile successfully?
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
you are trying to use spring-boot, so read more about spring-boot here: getting started.
And see how it is done.
The simplest pom.xml you could have is something like this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.0.RC3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>