I set up a spring boot vaadin project and implemented this class according to springs tutorial, but cannot access the ui with localhost:8080/ui. What am I doing wrong?
My UI class:
package net.lawyd.server.ui;
import com.vaadin.annotations.Theme;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Notification;
import com.vaadin.ui.UI;
#SpringUI(path = "/ui")
#Theme("valo")
public class VaadinUI extends UI {
#Override
protected void init(VaadinRequest request) {
setContent(new Button("Click me", e -> Notification.show("Hello Spring+Vaadin user!")));
}
}
Here is my Spring configuration class:
package net.lawyd.server.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#Configuration
#ComponentScan("net.lawyd.server")
#EnableJpaRepositories("net.lawyd.server")
#EnableTransactionManagement
public class SpringConfig {
// Nothing to do here
}
Here 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>net.lawyd</groupId>
<artifactId>lawyd-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
[UPDATE 1] I noticed this log message:
Cannot enhance #Configuration bean definition
'com.vaadin.spring.VaadinConfiguration' since its singleton instance
has been created too early. The typical cause is a non-static #Bean
method with a BeanDefinitionRegistryPostProcessor return type:
Consider declaring such methods as 'static'.
[UPDATE 2] I created an empty spring boot application using start.spring.io and inserting the demo from the tutorial. It works there, so it has to be something with my setting.
[UPDATE 3] I figured out it has something to do with the JerseyConfig, when I take it out it Vaadin works, but jersey does not anymore:
#SpringBootConfiguration
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(TodoResource.class);
register(HealthResource.class);
}
}
Here is also the code on github.
I found the root problem: Jersey was mapped to the root, so all the calls on root were intercepted. Changing the #ApplicationPath("/api") to something other than root solved the problem:
#SpringBootConfiguration
#ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
Related
Currently I'm facing an issue in Autowire configuration between Repository and the service layer.
I'm unable to trace my mistakes.
Repository - OrderRepository
package com.kakashi.orderservice.repository;
import com.kakashi.orderservice.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface OrderRepository extends JpaRepository\<Order, Long\> {
}\`
Service - OrderService
package com.kakashi.orderservice.service;
import com.kakashi.orderservice.dto.OrderLineItemsDto;
import com.kakashi.orderservice.dto.OrderRequest;
import com.kakashi.orderservice.model.Order;
import com.kakashi.orderservice.model.OrderLineItems;
import com.kakashi.orderservice.repository.OrderRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
#Service
#RequiredArgsConstructor
public class OrderService {
private final OrderRepository orderRepository;
public void placeOrder(OrderRequest orderRequest) {
Order order = new Order();
order.setOrderNumber(UUID.randomUUID().toString());
List<OrderLineItems> orderLineItems = orderRequest.getOrderLineItemsDtoList()
.stream()
.map(orderLineItemsDto -> mapToDto(orderLineItemsDto)).toList();
order.setOrderLineItemsList(orderLineItems);
orderRepository.save(order) ;
}
private OrderLineItems mapToDto(OrderLineItemsDto orderLineItemsDto) {
OrderLineItems orderLineItems = new OrderLineItems();
orderLineItems.setPrice(orderLineItemsDto.getPrice());
orderLineItems.setQuantity(orderLineItemsDto.getQuantity());
orderLineItems.setSkuCode(orderLineItemsDto.getSkuCode());
return orderLineItems;
}
}
`
But I am getting the Below error:#
\`\*\*\*\*\*\*\*\*\*\*\*\*\
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.kakashi.orderservice.service.OrderService required a bean of type 'com.kakashi.orderservice.repository.OrderRepository' that could not be found.
Action:
Consider defining a bean of type 'com.kakashi.orderservice.repository.OrderRepository' in your configuration.
Please do guide me I am using java 17, here I am pasting my application.properties file and pom.xml for your reference
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/order-service
spring.datasource.username=root
spring.datasource.password=""
spring.jpa.hibernate.ddl-auto=update
server.port=8081
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>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kakashi</groupId>
<artifactId>order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Previously it was working fine, but I dunno why its misbehaving like this.
Try adding:
#RequiredArgsConstructor(onConstructor = #__(#Autowired))
on service class hope it would work.
Error: I get the following in Firefox: Foreign Site Query Blocked: The same origin policy does not allow reading of the remote resource http: // localhost: 8080 / api / v1 / post /. (Cause: The 'Access-Control-Allow-Origin' CORS header does not exist)
I have spent several hours to allow CORS communication with my Spring Boot server in order to make my REACT UI communicate with the server. There are lot of similarly phrased questions on Stack Overflow, but non of the suggested solutions have solved my problem...
Spring Boot suggests different solutions on https://spring.io/guides/gs/rest-service-cors/. One local solution is using annotations. A global solution can be obtained with a config file.
I have tried annotating the controller with #CrossOrigin as seen in my code below:
package com.example.Blogging.api;
import com.example.Blogging.model.Post;
import com.example.Blogging.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
#CrossOrigin(origins = "http://localhost:3000")
#RequestMapping("api/v1/post")
#RestController
public class PostController {
private final PostService postService;
#Autowired
public PostController(PostService postService) {
this.postService = postService;
}
#PostMapping
public void addPost(#RequestBody Post post){
postService.addPost(post);
}
#GetMapping
public List<Post> getAllPosts(){
return postService.getAllPosts();
}
#GetMapping(path = "{id}")
public Optional<Post> getPostById(#PathVariable("id") UUID id){
return postService.getPostById(id);
}
#PutMapping(path="{id}")
public void updatePostById(#PathVariable("id") UUID id, #RequestBody Post post){
postService.updatePost(id,post);
}
#DeleteMapping(path="{id}")
public void deletePostById(#PathVariable("id") UUID id){
postService.deletePost(id);
}
}
This did not work.. I have also tried annotating on each method instead of the whole controller class.
Furthermore I have tried to make a config file:
package com.example.Blogging.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*");
}
}
Nothing really seems to work. The console in my browser keeps saying 'access-control-allow-origin' is missing in the response header..
For reference, I will also put my pom.xml below:
<?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.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Blogging</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Blogging</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>
<!-- SPRING SECURITY -->
<!--<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<scope>test</scope>
</dependency>-->
<!-- SPRING BOOT STARTER SECURITY -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</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-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
</plugins>
</build>
</project>
Anyone who can suggest a solution? Thank you in advance!
I just noticed, you have Spring Security in your dependencies.
You might missed to enable CORS in your WebSecurityConfig.
Eg. something like this:
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and()...
}
}
I'm new in Spring Boot, I'm trying to build a simple application with REST Api, and I'm getting this error immediately when starting the app:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-24 20:08:19.505 ERROR 11344 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.requestMappingHandlerAdapter(WebMvcAutoConfiguration.java:369)
The following method did not exist:
'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.requestMappingHandlerAdapter(org.springframework.web.accept.ContentNegotiationManager, org.springframework.format.support.FormattingConversionService, org.springframework.validation.Validator)'
The method's class, org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration, is available from the following locations:
jar:file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar!/org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class
The class hierarchy was loaded from the following locations:
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport: file:/C:/Users/user/.m2/repository/org/springframework/spring-webmvc/5.1.3.RELEASE/spring-webmvc-5.1.3.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
Process finished with exit code 0
I did not finish yet with the development of the app, but I can't go on because of this errors.
There are my classes so far:
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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>micro1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>micro1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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>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>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <version>1.4.200</version>-->
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</build>
</project>
controller:
package com.example.micro1.controllers;
import com.example.micro1.entities.User;
import com.example.micro1.services.Service;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
#RestController
#RequestMapping("/users")
#Data
public class Controller {
private final Service service;
#PostMapping
User addNewUser(#RequestBody User user){
return service.add(user);
}
}
service:
package com.example.micro1.services;
import com.example.micro1.entities.User;
import com.example.micro1.repositories.Repository;
import lombok.Data;
#org.springframework.stereotype.Service
#Data
public class Service {
private final Repository repository;
public User add(User user){
return repository.save(user);
}
}
repository:
package com.example.micro1.repositories;
import com.example.micro1.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
#org.springframework.stereotype.Repository
public interface Repository extends JpaRepository<User, Integer> {
#Override
<S extends User> S save(S s);
}
One entity for now:
package com.example.micro1.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
private String phoneNumber;
}
application.properties:
#h2
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
The application class:
package com.example.micro1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Micro1Application {
public static void main(String[] args) {
SpringApplication.run(Micro1Application.class, args);
}
}
Please tell me if there is something else that I should write here...
Thanks!
Your starter spring-boot-starter-web contains inside itself these dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency>
besides, you add in your pom.xml that:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
so, when you'll start your application you have in classpath ambiguity. I think it's cause of your problem.
Spring Boot starters save you the trouble of specifying explicitly declare dependency.
Try to remove spring-webmvc dependency.
I had this same issue when I run a Spring Boot Application of Web. But I got following error messages:
***************************
APPLICATION FAILED TO START
***************************
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.configureHandlerExceptionResolvers(WebMvcAutoConfiguration.java:519)
The following method did not exist:
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration.addDefaultHandlerExceptionResolvers(Ljava/util/List;)V
The method's class, org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration, is available from the following locations:
jar:file:/Users/itzhouq/dev/mavne_repository/org/springframework/boot/spring-boot-autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar!/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class
It was loaded from the following location:
file:/Users/itzhouq/dev/mavne_repository/org/springframework/boot/spring-boot-autoconfigure/2.1.6.RELEASE/spring-boot-autoconfigure-2.1.6.RELEASE.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration
This may be spring-boot-autoconfigure's exception.So, I checked the dependency of this.
org.springframework.boot:spring-boot-web:2.2.1.RELEASE--> org.springframework.boot:spring-boot-starter:2.2.1.RELEASE -->
org.springframework.boot:spring-boot-autoconfigure:2.2.1.RELEASE(omitted for conflict with 2.1.6.RELEASE). Obviously, the autoconfigure was losing because of version conflict.For solve this problem, I added the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
confirm this dependency had be added in the module and reruned the application.
Add one more dependency is Spring-webMVC because spring web MVC is using for deploying your project
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.3</version>
<scope>compile</scope>
</dependency
I am trying to refactor a Java program I wrote last year to use Spring Boot. The front end uses JavaFX, and so I am trying to use FxWeaver.
However, when I run my program, I get the following error on startup:
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'net.rgielen.fxweaver.core.FxWeaver' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
...
From rgielen's website, it shouldn't be necessary to provide an RxWeaver "... when using the Spring Boot Starter, since it provides auto-configuration for a FxWeaver instance."
So I'm stumped as to why I'm getting this error. Would anyone be able to take a look please?
Main Application
package application;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javafx.application.Application;
#SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
Application.launch(SpringbootJavaFxApplication.class, args);
}
}
SpringBootJavaFxApplication
package application;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
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;
public class SpringbootJavaFxApplication extends Application {
private ConfigurableApplicationContext applicationContext;
#Override
public void init() throws Exception {
this.applicationContext = new SpringApplicationBuilder()
.sources(MainApplication.class)
.run(getParameters().getRaw().toArray(new String[0]));
}
#Override
public void start(Stage stage) {
FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
Parent root = fxWeaver.loadView(Controller.class);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
#Override
public void stop() {
this.applicationContext.close();
Platform.exit();
}
}
pom
<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>xxxxx</groupId>
<artifactId>xxxxx</artifactId>
<version>1.0</version>
<name>xxxxx</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12.0.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.rgielen</groupId>
<artifactId>javafx-weaver-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>application.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(Obviously the pom contains things not specifically related to this issue, but I'm including the whole file in case anything else present is causing this issue.)
Please can you let me know if you have worked out what is causing the problem? I'm tearing my hair out with this now!
Also, apologies if I've not included the right/sufficient information. This is my first post on here. I am using eclipse.
Thank you!
Maybe I'm too late to help, but:
I re-created an example based on a minimal pom and your Java code. I could not re-produce your issue, though.
Here 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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>net.rgielen.sample</groupId>
<artifactId>fxweaver-springboot-starter-sample</artifactId>
<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-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>net.rgielen</groupId>
<artifactId>javafx-weaver-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
What concerns me with your pom
it adds a dependency on OpenJFX 12 (targeted at JDK 12), but compiles with target 1.8 - which in a Spring Boot application would be easier to define with the java.version property. When using a 1.8 JRE with embedded JavaFX (Oracle, Liberica) you would not need OpenJFX deps at all
You don't need javafx-maven-plugin when packaging as Spring Boot application.
That said, I fail to see this as root cause.
If you want to compare in detail, here's the code:
https://github.com/rgielen/fxweaver-springboot-stripped-demo
Just a brief update for anybody who finds this in future. I was unable to resolve the problem in the end, so instead went for a full refactor of the code, without the use of FxWeaver.
More labour-intensive, but the resulting code is arguably much better as a result.
I had the same problem.
I checked Spring conditions evaluation report and found this:
FxWeaverAutoConfiguration:
Did not match:
- #ConditionalOnClass did not find required class 'javafx.fxml.FXMLLoader' (OnClassCondition)
So I put dependency on javafx-fxml
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>12.0.2</version>
</dependency>
And the condition was satisfied.
This worked for me.
I have a basic spring boot RESTful application which works fine but when I try to add authorization using spring-boot-starter-security the password is not printed on the console. Following are the contents of the relevant files
application.properties
logging.file=route.log
logging.level.org.springframework.web=ERROR
logging.level.org.springframework.boot.test=ERROR
logging.level.org.springframework.boot.autoconfigure.security=INFO
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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.be.test</groupId>
<artifactId>testName</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.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>
<start-class>com.be.test.TestNameApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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>
<!-- https://mvnrepository.com/artifact/com.google.maps/google-maps-services -->
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>0.1.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
controller class
package com.be.test.api;
#RestController
public class TestController {
#RequestMapping(value = "/test", method=RequestMethod.GET)
public
#ResponseBody
long test() {
return 1000;
}
}
Main class
package com.be.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication()
#ComponentScan
#EnableAutoConfiguration
public class TestNameApplication {
public static void main(String[] args) throws Exception {
ApplicationContext ctx = SpringApplication.run(TestNameApplication.class, args);
}
}
Is there anything missing from any of the files ?
Add
logging.level.org.springframework.security=INFO
to your application.properties file. This came from the first note on this page, and an example of it is here.
You add #EnableAutoConfiguration to file TestNameApplication
#EnableWebSecurity
public class TestNameApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(TestNameApplication .class, args);
}
}
You can run file TestNameApplication, then you can see pass in console log IDE.
Spring will auto generate password belong to format:
Using default security password: a72f8e06-d1ac-4211-a84d-933534359c52