I tried to run the application and It fails with required beans - java

Description:
Field userRepository in com.rst.boot.services.impl.UserServiceImpl required a bean named 'entityManagerFactory' that could not be found.
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
This is my service class:
#Service
public class UserServiceImpl implements UserServices {
#Autowired
private UserRepository userRepository;
#Override
public List<UserDTO> findAllUsers() {
List<UserDTO> allusers= userRepository.findAll();
return allusers;
}
#Override
public String saveUser(UserDTO userdata) {
userRepository.save(userdata);
return "Data saved";
}
}
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>org.example</groupId>
<artifactId>Leaning</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Springboot Demo App</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<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>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>

First, in your bootstrap class make sure you have the #EnableAutoConfiguration annotation. If you have #SpringBootApplication, should be enough, since the latter is annotated with the first.
#SpringBootApplication
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
Second, make sure that UserRepository extends JpaRepository or CrudRepository:
public interface UserRepository extends JpaRepository<User, Long> {}
Last, check that you have the correct dependency in your maven or gradle configuration. E.g.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Related

(Resolvido) Mock Mvc is always null

I was trying to implement a controller test to guarantee that it would return the http 200 code, but when trying to run the test I received a NullPointerException exception.
Debugging the code I found that the MockMvc was null.
I already tried to add annotations like #SpringBootTest, #AutoConfigureMockMvc, #WebMvcTest, #ExtendWith and the MockMvc was still null and now I don't know what else to do to solve this problem
Note: I'm learning about these test things so if there's something bad in the code I'd appreciate it if you let me know
Here is my test code:
`
#ExtendWith(MockitoExtension.class)
#WebMvcTest(BookController.class)
#AutoConfigureMockMvc
#SpringBootTest
public class TestControlBook {
#Autowired
private MockMvc mockMvc;
#Test
public void testListBooks() throws Exception{
Response ResultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/books/list"));
response.andExpect(MockMvcResultMatchers.status().isOk());
}
}
here is my controller code:
`#RestController
#RequestMapping("/api/books")
public class BookController {
private BookService bookService;
public BookController(BookService bookService) {
this.bookService = bookService;
}
#GetMapping("/list")
private ResponseEntity<Object> listBooks(){
try {
return ResponseEntity.status(HttpStatus.OK).body(bookService.listBooks());
}
catch (Exception e){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while trying to list the books, please try again later");
}
}
}
and 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 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.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tdd</groupId>
<artifactId>exemplo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TDD Exemplo</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-web</artifactId>
<version>3.0.0</version>
</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.6.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>`
```
I managed to solve the problem, what was happening was that the annotations were in conflict, causing the MockMvc not to be assigned. In addition, I changed some things so that the code was more readable and better.
The code is now like this:
#WebMvcTest(LivroController.class)
#AutoConfigureMockMvc
public class LivroControlerTeste {
#Autowired
private MockMvc mockMvc;
#MockBean
private LivroService livroService;
#Test
public void testarListarLivros() throws Exception{
MvcResult mvcResult = mockMvc.perform(get("/api/livros/listar"))
.andExpect(status().isOk()).andReturn();
}
}

Facing o.s.web.servlet.PageNotFound : No mapping for GET / problem in my spring boot project

I am trying to implement REST microservices using Spring Boot in my project. I have created a controller for login page and also created repository.
But while hitting URL I am facing this issue o.s.web.servlet.PageNotFound: No mapping for GET /
Main Class
#ComponentScan(basePackages = {"com.springboot.controller.repository"})
#EntityScan(basePackages="com.springboot.controller.model")
#EnableJpaRepositories(basePackages="com.springboot.controller.repository")
#SpringBootApplication
public class CunsultustodayWebServicesApplication {
public static void main(String[] args) {
SpringApplication.run(CunsultustodayWebServicesApplication.class, args);
}
}
Controller
#RestController
#ComponentScan
#Controller
public class LoginController {
#Autowired(required=true)
private UserRepo userrepo;
#RequestMapping("/")
public String checkMVC()
{
return "Login";
}
#RequestMapping("/login")
public String loginHome(#RequestParam("email") String email, #RequestParam("password") String password, Model model)
{
User u = null;
try {
u= userrepo.findByEmail(email);
}
catch(Exception ex) {
System.out.println("User Not Found!!!");
}
if(u!=null) {
model.addAttribute("email", email);
return "HomePage";
}
return "Login";
}
}
Repository
#Service("UserRepo")
public interface UserRepo extends JpaRepository<User,Integer> {
User findByEmail(String email);
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot.controller</groupId>
<artifactId>cunsultustoday-web-services</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cunsultustoday-web-services</name>
<description>Demo project for Spring Boot</description>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Please help me to find solution. Thank You
Your controller is probably not being picked up by the ComponentScan because you have limited to the package for the scan.
Try this:
// note #ComponentScan is removed as it is included already in #SpringBootApplication
#EnableJpaRepositories
#SpringBootApplication
public class CunsultustodayWebServicesApplication {
...
And This:
// note #Controller and #CompnentScan are removed
#RestController
public class LoginController {

MockMvc returned HttpMessageNotWritableException with application/json

I have a rest endpoint with spring 2.3.4.RELEASE
When I run test for controller with MockMvc I received
w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.http.converter.HttpMessageNotWritableException:
No converter for [class com.example.myexample.model.User] with preset
Content-Type 'application/json']
#SpringBootTest(classes = UserController.class)
#ExtendWith(SpringExtension.class)
#AutoConfigureMockMvc
public class UserControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private UserRepository userRepository;
private static final String GET_USERBYID_URL = "/users/{userId}";
#Test
public void shouldGetUserWhenValid() throws Exception {
Address address = new Address();
address.setStreet("1 Abc St.");
address.setCity("Paris");
User userMock = new User();
userMock.setFirstname("Lary");
userMock.setLastname("Pat");
userMock.setAddress(address);
when(userRepository.findById(1)).thenReturn(Optional.of(userMock));
mockMvc.perform(get(GET_USERBYID_URL, "1").accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
}
#RestController
#RequestMapping(path = "/users")
#Slf4j
public class UserController {
#Autowired
private UserRepository userRepository;
#GetMapping(value = "/{userId}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(#PathVariable("userId") Integer userId) {
log.info("Getting user with id={}", userId);
final Optional<User> userOptional = userRepository.findById(userId)
return userOptional.map(value -> ResponseEntity.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(value))
.orElseGet(() -> ResponseEntity.noContent()
.build()).ok()
.body(userOptional.get());
}
}
#Data
public class User {
private String firstname;
private String lastname;
private Address address;
}
#Data
public class Address {
private String street;
private String city;
}
#Repository
public interface UserRepository extends CrudRepository<User, Integer> {
}
I've read other article that recommended to added jackson dependency, but this doesn't work for me.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.3</version>
</dependency>
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>myexample</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<lombok.version>1.18.12</lombok.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-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.3</version>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If I run the application and test it through postman, the endpoint works fine.
Any ideas why I get this error message on my rest controller test?
The problem lies in your #SpringBootTest, when you added classes = UserController.class , it tells spring to create a context with UserController.class. So it create spring context only with UserController. As a result only your controller bean is created and other beans needed for Http Message Converting is not created. So one solution is removing classes = UserController.class from #SpringBootTest.
#SpringBootTest
#ExtendWith(SpringExtension.class)
#AutoConfigureMockMvc
Now you should have no problem running the tests.
But this comes with another problem, now this will load the full spring context which is not needed for only Controller Layer Testing. It will also increase the time of your unit tests.
To solve this problem , spring has another annotation #WebMvcTest . If you annotate yous tests with #WebMvcTest, Spring Boot instantiates only the web layer rather than the whole context. You can also choose to instantiate only one controller. For example,
#ExtendWith(SpringExtension.class)
#WebMvcTest(UserController.class)
public class UserControllerTest {
}
This should solve your problem.
Apparently I put the UserControllerTest in a wrong package name.
It's working now after I put the UserControllerTest in the same package name as the UserController.

dispatcherServletRegistration Spring boot exception

Help please, I try to put Vaadin on my (Spring boot) server and after I set up a simple example, I get the following error:
The bean 'dispatcherServletRegistration', defined in class path resource [com/vaadin/flow/spring/SpringBootAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration.class] and overriding is disabled.
Then, to correct the error, I write in application.prop:
spring.main.allow-bean-definition-overriding=true
and get the following error:
Parameter 1 of constructor in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
POM.XML :
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.lopamoko
cloudliquid
0.0.1-SNAPSHOT
jar
<name>cloudliquid</name>
<description>Demo project for Spring Boot</description>
<pluginRepositories>
<pluginRepository>
<id>maven-annotation-plugin-repo</id>
<url>http://maven-annotation-plugin.googlecode.com/svn/trunk/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from com.lopamoko.cloudliquid.repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>ru.leon0399</groupId>
<artifactId>dadata</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-slf4j</artifactId>
<version>9.3.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</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-data-jpa</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry</artifactId>
<version>1.7.16</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin-bom</artifactId>
<version>${vaadin.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>
Its my Main application:
#SpringBootApplication()
#EntityScan("com/lopamoko/cloudliquid/dataModel")
#EnableJpaRepositories(basePackages = "com.lopamoko.cloudliquid.repository")
public class CloudliquidApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(CloudliquidApplication.class, 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);
}
FirebaseConfig firebaseConfig = new FirebaseConfig();
firebaseConfig.configurateFirebaseApplication();
}
#Bean
public DadataService dadataService() {
return new DadataServiceImpl();
}
#Bean
public DadataClient dadataClient() {
return Feign.builder()
.client(new OkHttpClient())
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.logger(new Slf4jLogger(DadataClient.class))
.logLevel(Logger.Level.FULL)
.target(DadataClient.class, "https://suggestions.dadata.ru/suggestions/api/4_1/rs/suggest/address");
}
#Bean
public CustomerDeliveryService customerDeliveryService() {
return new CustomerDeliveryServiceImpl();
}
#Bean
public OrderService orderService() {
return new OrderServiceImpl();
}
#Bean
public YandexService yandexService() {
return new YandexServiceImpl();
}
#Bean
public CategoryService categoryService() {
return new CategoryServiceImpl();
}
#Bean
public ShopService shopService() {
return new ShopServiceImpl();
}
#Bean
public CommentService commentService() {
return new CommentServiceImpl();
}
#Bean
public RatingService ratingService() {
return new RatingServiceImpl();
}
#Bean
public ProductService productService() {
return new ProductServiceImpl();
}
#Bean
public TomcatServletWebServerFactory tomcatFactory() {
return new TomcatServletWebServerFactory() {
#Override
protected void postProcessContext(Context context) {
((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
}
};
})
And Vaadin sample:
#Route("/TestMySelf")
public class MainView extends VerticalLayout {
public MainView() {
Label label = new Label("Hello its CloudLiquid");
}
}
What is the version of vaadin-spring-boot-starter you are using ?
From this , it said this is the known issues when certain old version of vaadin-spring-boot-starter is used with Spring Boot 2.1 and it will be fixed in some newer release :
We have another working workaround and are also close to finding the
proper fix without the need for the workaround. Regardless which
approach we use first, next week we should have platform releases that
work with Spring Boot 2.1. (v10, v11 and v12)
Sorry for keeping you on hold before being able updating to Spring
Boot 2.1, we should have looked at this earlier - my bad.
Then from the release note here, it said version 10.1.0 supports Spring Boot 2.1. So my advice is to upgrade the vaadin-spring-boot-starter to at least 10.1.0 or the latest version.

Spring : Restrict injection of Repository module in Controller

I'm building a REST Webservice using Spring Boot (v 1.4.2.RELEASE). I'm trying to follow separation of concerns pattern, so I've created 3 maven projects,
myapp-repository, myapp-service and myapp-rest. myapp-service has a dependency of myapp-repository and myapp-rest has a dependency of myapp-service. Below is the initial configuration I've made.
myapp-repository
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
Repository Config
#Configuration
#EnableJpaRepositories
#ComponentScan("com.mycompany.myapp.repository")
#EntityScan("com.mycompany.myapp.entity")
#PropertySource("classpath:repository.properties")
public class RepositoryConfig {
#Bean
#Primary
#ConfigurationProperties(prefix = "myapp.primary.datasource")
public DataSource getDataSource() {
return DataSourceBuilder.create().build();
}
}
myapp-service
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mycompany.myapp</groupId>
<artifactId>myapp-repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Service config
#Configuration
#ComponentScan("com.mycompany.myapp.service")
#Import(RepositoryConfig.class)
#PropertySource("classpath:service.properties")
public class ServiceConfig {
}
myapp-rest
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mycompany.myapp</groupId>
<artifactId>myapp-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Rest config
#SpringBootApplication
#ComponentScan("com.mycompany.myapp.rest")
#Import(ServiceConfig.class)
public class RestConfig extends SpringBootServletInitializer {
/**
* #param args
*/
public static void main(String[] args) {
SpringApplication.run(RestConfig.class, args);
}
}
Now, I want to restrict my repository services to be autowired in myapp-rest, but with this current implementation, it's possible. Only services inside myapp-service module should be allowed to autowire in myapp-rest. Is it possible?

Categories