I'm coding microservices app based on Spring Cloud. I launched Eureka Server and now i'm coding a car-service. It worked when I didn't have any Autowiring in project. After adding Repository, Service and changing Controller the car-service doesn't launch.
When I added #SpringBootApplication("com.carrental.carservice.repository") application started but Rest API doesn't work and return 404. I tried with #Qualifier and naming Repository but still doesn't work.
There is an error when starting:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.carrental.carservice.service.impl.CarTypeServiceImpl required a bean of type 'com.carrental.carservice.repository.CarTypeRepository' that could not be found.
Action:
Consider defining a bean of type 'com.carrental.carservice.repository.CarTypeRepository' in your configuration.
And there is a WARN in logs:
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.carrental.carservice.repository.CarTypeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
pom.xml of car service
<?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>
<artifactId>carrental</artifactId>
<groupId>com.carrental</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>car-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.9.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.199</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
</project>
starting application file
package com.carrental.carservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#EnableEurekaClient
//#ComponentScan("com.carrental.carservice.repository")
public class CarServiceApp {
public static void main(String[] args) {
SpringApplication.run(CarServiceApp.class, args);
}
}
controller
package com.carrental.carservice.controller;
import com.carrental.carservice.dto.CarTypeDto;
import com.carrental.carservice.model.entity.CarType;
import com.carrental.carservice.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
#RestController
#RequestMapping("/car")
public class CarTypeController {
private final CarTypeService carTypeService;
#Autowired
public CarTypeController(CarTypeService carTypeService){
this.carTypeService = carTypeService;
}
#PostMapping("/cartype/add")
public ResponseEntity<CarTypeDto> addCarType(#Valid #RequestBody CarTypeDto dto){
CarType entity = Mapper.mapToCarTypeEntity(dto);
this.carTypeService.add(entity);
return new ResponseEntity<CarTypeDto>(Mapper.mapToCarTypeDto(entity), HttpStatus.CREATED);
}
#GetMapping
public String get(){
return "jajo";
}
}
service
package com.carrental.carservice.service.impl;
import com.carrental.carservice.model.entity.CarType;
import com.carrental.carservice.repository.CarTypeRepository;
import com.carrental.carservice.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
#Service
public class CarTypeServiceImpl implements CarTypeService {
private final CarTypeRepository carTypeRepository;
#Autowired
public CarTypeServiceImpl(CarTypeRepository carTypeRepository){
this.carTypeRepository = carTypeRepository;
}
#Override
public void add(CarType carType) {
if(this.carTypeRepository.existsByName(carType.getName()))
return;
this.carTypeRepository.save(carType);
}
#Override
public List<CarType> getAll() {
return this.carTypeRepository.findAll();
}
#Override
public void update(CarType carType) {
if(this.carTypeRepository.existsById(carType.getId()))
this.carTypeRepository.save(carType);
}
#Override
public void delete(CarType carType) {
this.carTypeRepository.delete(carType);
}
}
repository
package com.carrental.carservice.repository;
import com.carrental.carservice.model.entity.CarType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface CarTypeRepository extends JpaRepository<CarType, Long> {
boolean existsByName(String name);
CarType getByName(String name);
boolean existsById(Long id);
}
I tried a lot of stuff and still doesn't work. Can you help, please?
i think you need to use feignClient to avoid this exception.i had similar issue and added
#EnableFeignClients to my ApplicationClass and its dependency to your pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId
</dependency>
Related
Ive been trying to follow a few basic examples to create a rest api to connect to a local mysql server
However I keep running to this error
Consider defining a bean of type 'org.example.database.MessagesRepository' in your configuration.
Here is my current project layout
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>org.example</groupId>
<artifactId>messagesApi</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</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-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Main.java
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
#EnableAsync
#SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
MessagesApi
package org.example.api;
import org.example.models.Messages;
import org.example.database.MessagesRepository;
import org.example.models.MessagesRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
#Controller
#RequestMapping("/chats")
public class MessagesApi {
#Autowired
MessagesRepository messagesRepository;
#GetMapping("id")
public ResponseEntity<Messages> getAllChatsById(#RequestBody MessagesRequest messageRequest) {
return new ResponseEntity<>(messagesRepository.findByTextId(messageRequest.getTextId()), HttpStatus.OK);
}
#GetMapping("username")
public ResponseEntity< List<Messages>> getAllChatsByUserName(#RequestBody MessagesRequest messageRequest) {
return new ResponseEntity<>(messagesRepository.findByUserName(messageRequest.getUserName()), HttpStatus.OK);
}
#PostMapping
public ResponseEntity<Long> postMessage(#RequestBody Messages message ){
//make Id
Long id = 1234L;
message.setTextId(id);
messagesRepository.save(message);
return new ResponseEntity<>(id, HttpStatus.CREATED);
}
}
MessagesRespository
package org.example.database;
import org.example.models.Messages;
import org.springframework.data.repository.JpaRepository;
import java.util.List;
public interface MessagesRepository extends JpaRepository<Messages, Long> {
List<Messages> findByUserName(String userName);
Messages findByTextId(Long textId);
}
applications.properties
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=passWord
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Ive tried adding
#EnableJpaRepositories
But kept getting
``Consider defining a bean named 'entityManagerFactory' in your configuration.
When removing
exclude={DataSourceAutoConfiguration.class}
I would get
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
I am not sure where to move forward from here
public interface MessagesRepository extends JpaRepository<Messages, Long> {
rather should be
#Repository
public interface MessagesRepository extends JpaRepository<Messages, Long> {
to make below code work
#Autowired
MessagesRepository messagesRepository;
also exclude from main class
exclude={DataSourceAutoConfiguration.class}
Sometimes spring is not able to scan your entity classes or your configuration classes so use this it will work fine
#SpringBootApplication
#ComponentScan({"org.example"})
#EntityScan("org.example")
#EnableJpaRepositories("org.example")
public class WebServiceApplication extends SpringBootServletInitializer {
}
Thank you in advance, Hello I am new to spring boot. I am facing a problem with spring boot. When I run my URL in postman it shows an error below. I have also uploaded my code below. I don't find any idea what wrong with my code.
{
"timestamp": "2021-07-09T06:43:57.429+00:00",
"status": 404,
"error": "Not Found",
"path": "/todo"
}
Controller code
This is a code for my controller code.
package com.assignment.todo.Todo.Controller;
import com.assignment.todo.Todo.Repo.*;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.assignment.todo.Todo.Model.*;
import com.assignment.todo.Todo.Repo.*;
#RestController
#RequestMapping(value="/todo")
public class TodoController {
#Autowired
private TodoRepo todoRepo;
#GetMapping
public List<TodoModel> findAll(){
return todoRepo.findAll();
}
#PostMapping
public TodoModel save(#Valid #NotNull #RequestBody TodoModel todoItem) {
return todoRepo.save(todoItem);
}
}
Model
code for my model class
package com.assignment.todo.Todo.Model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.validation.constraints.NotBlank;
import org.springframework.data.annotation.Id;
#Entity
public class TodoModel {
private Long id;
#NotBlank
private String title;
private boolean done;
public TodoModel() {
}
public TodoModel(long id, String title, boolean done) {
this.id=id;
this.title=title;
this.done=done;
}
#Id
#GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isDone() {
return done;
}
public void setDone(boolean done) {
this.done = done;
}
}
Repo
code for my repo class
package com.assignment.todo.Todo.Repo;
import com.assignment.todo.Todo.Model.*;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TodoRepo extends JpaRepository<TodoModel, Long> {
}
MainClass
package com.assignment.todo.Todo;
import com.assignment.todo.Todo.Controller.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import com.assignment.todo.Todo.Repo.*;
#SpringBootApplication(scanBasePackages = "TodoRepo.java")
public class TodoApplication {
public static void main(String[] args) {
SpringApplication.run(TodoApplication.class, args);
}
}
pom.xml
My pom.xml file
<?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.assignment.todo</groupId>
<artifactId>Todo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Todo</name>
<description>Assignment in Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>3.1.4</version>
</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>
<!-- https://mvnrepositosry.com/artifact/org.springframework/spring-web -->
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.istack/maven-istack-commons-plugin -->
<!-- https://mvnrepository.com/artifact/com.sun.istack/maven-istack-commons-plugin -->
<dependency>
<groupId>com.sun.istack</groupId>
<artifactId>maven-istack-commons-plugin</artifactId>
<version>2.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#RestController("/todo")// you can give any URL here
public class TodoController {
#Autowired
private TodoRepo todoRepo;
#GetMapping("/getAll")// you can give any URL here
public List<TodoModel> findAll(){
return todoRepo.findAll();
}
#PostMapping("/save")// you can give any URL here
public TodoModel save(#Valid #NotNull #RequestBody TodoModel todoItem) {
todoRepo.save(todoItem);
return todoItem;
}
}
you give the mapping to the method like this. you can give any URL name. This is the main reason you get a 404 error.
There is issue in your mapping. You didn't specify the mapping path so findAll and save has default mapping as empty then controller could not understand which API you calling. Add below things and try.
#GetMapping(value="/findAll")
#PostMapping(value=/save")
The TodoApplication class must be above this one level.
change this:
from
package com.assignment.todo.Todo;
to
package com.assignment.todo;
I have an error that I can't track down. I'm new so sorry if I missed this in the searches, but I tried several things and no luck.
UserApi.java:
package com.jsp.jsp;
import com.jsp.models.User;
import com.jsp.services.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
#RestController
class UserApi {
private final UserService userService;
public UserApi(UserService userService) {
this.userService = userService;
}
#RequestMapping(value="/api/user", method=RequestMethod.POST)
public User createUser(
#RequestParam(value="name", required=true) String name,
#RequestParam(value="email", required=true) String email,
#RequestParam(value="password", required=true) String password,
#RequestParam(value="confirm", required=true) String confirm)
{
User u = this.userService.createUser(new User(name, email, password));
return u;
}
}
UserRepository.java:
package com.jsp.repositories;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
import com.jsp.models.User;
import org.springframework.data.repository.CrudRepository;
#Repository
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByEmail(String email);
Optional<User> findById(Long id);
List<User> findAll();
}
Server.java
package com.jsp.jsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#SpringBootApplication(scanBasePackages={"com.jsp.models","com.jsp.repositories","com.jsp.services"})
#RestController
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}
UserService.java
package com.jsp.services;
import java.util.List;
import com.jsp.models.User;
import com.jsp.repositories.UserRepository;
import org.springframework.stereotype.Service;
#Service
public class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public List<User> allUsers() {
return this.userRepository.findAll();
}
public User createUser(User u) {
return this.userRepository.save(u);
}
}
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.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.jsp</groupId>
<artifactId>jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</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>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>jbcrypt</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.11.7.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
So in short, when I run the above project in VSCode, it errors out with the following:
2019-02-23 19:07:52.744 WARN 25412 --- [ restartedMain]
ConfigServletWebServerApplicationContext : Exception encountered
during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userService' defined in file
[C:\Users\Alex\Documents\dojo\javatown\everything\target\classes\com\jsp\services\UserService.class]:
Unsatisfied dependency expressed through constructor parameter 0;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'com.jsp.repositories.UserRepository'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations: {}
My understanding is that the repository I made should be automatically turned into a bean and then wire itself up to the service I made. However, that seems to be not happening. My example videos are using MySQL -- will that make a difference? Am I using the right driver for Postgres 11? I'm super lost.
Some remarks and suggestions:
1.add autowired annotation on your constructor, it is clearer what you need spring to do.
#Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
2. I don't understand #RestController annotation on your main class.
#SpringBootApplication(scanBasePackages {"com.jsp.models","com.jsp.repositories","com.jsp.services"})
#RestController --> THIS CAN BE REMOVED
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}
3.You should add #EnableJpaRepositories("com.jsp.repositories") on your spring boot application => it will scan the repositories in that package.
#SpringBootApplication(scanBasePackages {"com.jsp.models","com.jsp.repositories","com.jsp.services"})
#EnableJpaRepositories("com.jsp.repositories")
public class Server {
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}
I was trying out a sample springboot project. But in the half way i got stuck up with this problem.
My Controller.java
package org.springbootdemo5.springbootdemo5.controler;
import org.springboot5.springbootdemo5.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class SampleRestController {
#Autowired
private TaskService taskService;
#GetMapping("/hello")
public String hello(){
return "Hello World!!!";
}}
My Task Service
package org.springboot5.springbootdemo5.service;
import java.util.ArrayList;
import java.util.List;
import javax.transaction.Transactional;
import org.springboot5.springbootdemo5.dao.TaskRepository;
import org.springboot5.springbootdemo5.model.Task;
import org.springframework.stereotype.Service;
#Service
#Transactional
public class TaskService {
private final TaskRepository taskRepository;
public TaskService(TaskRepository taskRepository) {
this.taskRepository = taskRepository;
}
public List<Task> findAll(){
List<Task> tasks = new ArrayList<>();
for(Task task : taskRepository.findAll()){
tasks.add(task);
}
return tasks;
}
public Task findTask(int id){
return taskRepository.findOne(id);
}
public void save(Task task){
taskRepository.save(task);
}
public void delete(int id){
taskRepository.delete(id);
}}
My Task Repository
package org.springboot5.springbootdemo5.dao;
import org.springboot5.springbootdemo5.model.Task;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
public abstract class TaskRepository implements CrudRepository<Task, Integer>
{
}
And this my Initializer
package org.springbootdemo5.springbootdemo5;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan("org.springboot5.springbootdemo5")
public class Springbootdemo5Application {
public static void main(String[] args) {
SpringApplication.run(Springbootdemo5Application.class, args);
}}
All the annotations are provided properly and pom.xml is also correct but still i am getting exception like
Exception
encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'taskService' defined in file
Unsatisfied dependency
expressed through constructor parameter 0; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type 'org.springboot5.springbootdemo5.dao.TaskRepository'
available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {}
Description:
Parameter 0 of constructor in
org.springboot5.springbootdemo5.service.TaskService required a bean of type
'org.springboot5.springbootdemo5.dao.TaskRepository' that could not be
found.
Action:
Consider defining a bean of type
'org.springboot5.springbootdemo5.dao.TaskRepository' in your configuration.
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>org.springbootdemo5</groupId>
<artifactId>springbootdemo5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springbootdemo5</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.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-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-starter-web</artifactId>
</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>9.0.0.M26</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Anybody can solve this ?
Any help is always welcome.
Thank you.
Try changing your TaskRepository
to
public interface TaskRepository extends CrudRepository<Task, Integer>
Check the bean ID mentioned in the xml and the member in the Java file should be same , which is case sensitive.
config.xml
<bean id="reportServicePdf" class="my.local.spring.sprintlex.ReportService">
<constructor-arg name="numberOfPage" value="11" /> //<====Here
</bean>
ReportService.java:
public class ReportService {
#Value("100")
private int numberOfPage;//<====Here
::
::
}
I starting in Spring MVC with the Book Spring in Action, I'm making the exercise of the chapter 5, with the Spitter application but I got the following error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'spittleController' defined in file [C:\xampp\htdocs.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\spitter\WEB-INF\classes\com\spitter\web\SpittleController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.spitter.data.SpittleRepository]: : No qualifying bean of type [com.spitter.data.SpittleRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.spitter.data.SpittleRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
I got the project here in github: https://github.com/kevingcfcb88/spitter.git
I already do my research but nothing seems to work.
I'm using STS and Maven, this is the structure of the app:
Here is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spitter.config</groupId>
<artifactId>spitter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spitter</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>4.1.5.RELEASE</springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</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>spitter</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>spitter</finalName>
</build>
And my config files:
SpittrWebAppInitializer.java
package com.spitter.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
WebConfig.java
package com.spitter.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#Configuration
#EnableWebMvc
#ComponentScan("com.spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
RootConfig.java
package com.spitter.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#Configuration
#ComponentScan(basePackages = { "com.spitter.data" }, excludeFilters = {#Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {
}
As #mh-dev explained, you need an implementation of SpittleRepository. Try adding this class and see if your code can run:
public class SpittleRepositoryImpl implements SpittleRepository {
List <Spittle> findSpittles(long max, int count) {
System.out.println("I need a real implementation! "
+ "I received max as " + max + " and count as " + count + ".");
}
}
I would suggest re-reading the relevant sections of the book to make sure you didn't miss anything.
You just need to add a repository implementation of SpittleRepository with #Repository in the header, because even though you have added implementation classes, spring IOC is unaware of actual dependency and the example in the book should have worked if you had put all the java files in the same package hierarchy with repository implementation.
#Repository
public class SpittleRepositoryDAO implements SpittleRepository {
public SpittleRepositoryDAO() {
}
#Override
public List<Spittle> findSpittles(long max, int count) {
List<Spittle> spittles = new ArrayList<Spittle>();
for (int i = 0; i < count; i++) {
spittles.add(new Spittle("Spittle " + i, new Date()));
}
return spittles;
}
}
I know this code. It used to give me serious headache 2 years ago.
The eror is in the code OR the book itslef.
Go to the publishers site and download the updated sourcecode. here
or
https://www.manning.com/books/spring-in-action-fourth-edition
Here is the implementation of the repository:
package com.spitter.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.spitter.data.SpittleRepository;
#Controller
#RequestMapping("/spittles")
public class SpittleController {
private SpittleRepository spittleRepository;
#Autowired
public SpittleController(SpittleRepository spittleRepository) {
this.spittleRepository = spittleRepository;
}
#RequestMapping(method = RequestMethod.GET)
public String spittles(Model model) {
model.addAttribute("spittleList", spittleRepository.findSpittles(Long.MAX_VALUE, 20));
return "spittles";
}
}
yep, the problem is that you need an implementation of SpittleRepository. Cause you can't have an interface as bean, you know. The scan component need at least one implementation of it and with the annotation #Component. That's it ;)