「weatherController」という名前のBeanの作成中にエラーが発生しました:フィールド「weatherService」で表現された満たされていない依存関係。

mryunus:

Spring Data H2で基本的なSpring Boot REST API CRUDを開発しようとしています

エラーは:

org.springframework.beans.factory.UnsatisfiedDependencyException:「weatherController」という名前のBeanの作成中にエラーが発生しました:フィールド「weatherService」で表現された、満たされていない依存関係。入れ子の例外はorg.springframework.beans.factory.UnsatisfiedDependencyExceptionです。「weatherService」という名前のBeanの作成中にエラーが発生しました:フィールド「weatherRepository」で表現された、満たされていない依存関係。ネストされた例外はorg.springframework.beans.factory.BeanCreationException:「weatherRepository」という名前のBeanの作成エラー:initメソッドの呼び出しに失敗しました。ネストされた例外はjava.lang.IllegalArgumentExceptionです:メソッドpublic abstract java.util.List com.example.springboot2.WeatherRepository.findByNameContaining(java.lang.String)のクエリの作成に失敗しました!タイプWeatherのプロパティ名が見つかりませんでした!

コードを完成させましたが、機能せず、解決策が見つかりません。ヘルプが適用されます

Weather.java

    package com.example.springboot2;

import org.springframework.stereotype.Component;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDate;

@Entity
public class Weather {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)// Otomatik oluşturulmasın
    private long id;
    private String city;
    private LocalDate dateMeasured;
    private double tempMin;
    private double tempMax;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public LocalDate getDateMeasured() {
        return dateMeasured;
    }

    public void setDateMeasured(LocalDate dateMeasured) {
        this.dateMeasured = dateMeasured;
    }

    public double getTempMin() {
        return tempMin;
    }

    public void setTempMin(double tempMin) {
        this.tempMin = tempMin;
    }

    public double getTempMax() {
        return tempMax;
    }

    public void setTempMax(double tempMax) {
        this.tempMax = tempMax;
    }
}

WeatherController.java

package com.example.springboot2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/weather")
public class WeatherController{


    @Autowired
    private WeatherService weatherService;

    @GetMapping()
    public ResponseEntity<List<Weather>> getAllWeathers() {
        List<Weather> weatherList = weatherService.getAllWeathers();
        return new ResponseEntity<>(weatherList, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Weather> getWeatherById(
            @PathVariable("id") final Long id) {
        Weather weather = weatherService.getWeatherById(id);
        return new ResponseEntity<>(weather, HttpStatus.OK);
    }

    @PostMapping()
    public ResponseEntity<Weather> saveWeather(
            @RequestBody final  Weather weather) {
        Weather savedWeather = weatherService.saveWeather(weather);
        return new ResponseEntity<>(savedWeather, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Weather> updateWeatherById(
            @PathVariable("id") final Long id,
            @RequestBody final Weather weatherToUpdate) {
        Weather updatedWeather
                = weatherService.updateWeatherById(id, weatherToUpdate);
        return new ResponseEntity<>(updatedWeather, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteWeatherById(
            @PathVariable("id") final Long id) {
        weatherService.deleteWeatherById(id);
        return new ResponseEntity<>("Success", HttpStatus.OK);
    }

    @GetMapping("/search1/{searchString}")
    public ResponseEntity<List<Weather>> getWeatherByNameContaining(
            @PathVariable("searchString") final String searchString) {
        List<Weather> weatherList
                = weatherService.getWeatherByNameContaining(searchString);
        return new ResponseEntity<>(weatherList, HttpStatus.OK);
    }

    @GetMapping("/search2/{searchString}")
    public ResponseEntity<List<Weather>> getWeatherByNameLike(
            @PathVariable("searchString") final String searchString) {
        List<Weather> weatherList
                = weatherService.getWeatherByNameLike(searchString);
        return new ResponseEntity<>(weatherList, HttpStatus.OK);
    }





}

WeatherRepository

package com.example.springboot2;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;

import java.util.List;

public interface WeatherRepository extends CrudRepository<Weather,Long> {


    List<Weather> findByNameContaining(String value);
    @Query("SELECT w FROM Weather w WHERE w.city LIKE %:value%")
    List<Weather> findByNameLike(@Param("value") String value);

}

WeatherService

package com.example.springboot2;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("weatherService")
public interface WeatherService {

    List<Weather> getAllWeathers();
    Weather getWeatherById(Long id);
    Weather saveWeather(Weather weather);
    Weather updateWeatherById(Long id, Weather weatherToUpdate);
    void deleteWeatherById(Long id);
    List<Weather> getWeatherByNameContaining(String searchString);
    List<Weather> getWeatherByNameLike(String searchString);


}

WeatherServiceImpl

package com.example.springboot2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
@Component
@Service("weatherService")
public class WeatherServiceImpl implements WeatherService {

    @Autowired
    private WeatherRepository weatherRepository;

    @Override
    public List<Weather> getAllWeathers() {
        return (List<Weather>) weatherRepository.findAll();
    }

    @Override
    public Weather getWeatherById(final Long id) {
        return weatherRepository.findById(id).get();
    }

    @Override
    public Weather saveWeather(final Weather weather) {
        return weatherRepository.save(weather);
    }

    @Override
    public Weather updateWeatherById(
            final Long id, final Weather weatherToUpdate) {
        Weather weatherFromDb = weatherRepository.findById(id).get();
        weatherFromDb.setCity(weatherToUpdate.getCity());
        weatherFromDb.setDateMeasured(weatherToUpdate.getDateMeasured());
        weatherFromDb.setTempMax(weatherToUpdate.getTempMax());
        weatherFromDb.setTempMin(weatherToUpdate.getTempMin());
        return weatherRepository.save(weatherFromDb);
    }

    @Override
    public void deleteWeatherById(final Long id) {
        weatherRepository.deleteById(id);
    }

    @Override
    public List<Weather> getWeatherByNameContaining(final String searchString) {
        return weatherRepository.findByNameContaining(searchString);
    }

    @Override
    public List<Weather> getWeatherByNameLike(final String searchString) {
        return weatherRepository.findByNameLike(searchString);
    }
}

Springboot2Application

package com.example.springboot2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan("com.example")

public class Springboot2Application {

    public static void main(String[] args) {
        SpringApplication.run(Springboot2Application.class, args);
    }

}

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.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot2</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

エラーを解決するために私を助けてください

TroJaN:

あなたの中でWeatherRepository

List<Weather> findByNameContaining(String value);

これは、派生クエリメソッドです(詳細はこちら)。

問題は、Weatherエンティティにnameメソッドで使用している名前のプロパティがないことです。

メソッドは次のとおりです。

List<Weather> findByCityContaining(String value);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ