当前位置: 首页 > 知识库问答 >
问题:

创建名为“weather controller”的bean时出错:通过字段“weather service”表示的依赖项不满足;

孙弘博
2023-03-14

我已经完成了我的代码,但它不工作,我不能找到任何解决方案,任何帮助将得到认可

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);
    }





}
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);

}
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);


}
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);
    }
}
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);
    }

}
<?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>

请帮我解决这个错误

共有1个答案

谢俊悟
2023-03-14

weatherrepository中,您有:

List<Weather> findByNameContaining(String value);

现在,这是一个派生的查询方法(这里更多)。

问题是weather实体没有在方法中使用的名为name的属性。

List<Weather> findByCityContaining(String value);
 类似资料: