首页 > 解决方案 > Spring - 无法使用 CRUD 存储库保存实体

问题描述

我正在制作必须从 OpenWeatherMap API 获取一些信息并将其保存在 MySQL 数据库中的简单程序:

package com.example.restTemplate;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class RestTemplateApplication {
    public static void main(String[] args) {
        SpringApplication.run(RestTemplateApplication.class, args);
    }
    @Bean
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
package com.example.restTemplate.Model;

import lombok.*;

import javax.persistence.Entity;
import javax.persistence.Id;

@NoArgsConstructor
@AllArgsConstructor
@Entity
@javax.persistence.Table(name="Weather")
@Getter
@Setter
@ToString

public class WeatherScore {

    @Id
    private int id;
    private String city;
    private double temperature;
    private int  pressure;
    private long currentDate;

}
package com.example.restTemplate.Controller;


import com.example.restTemplate.Model.WeatherScore;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface WeatherCRUDRepository
        extends CrudRepository<WeatherScore, Long> {

    WeatherScore save (WeatherScore score);

}
package com.example.restTemplate.Controller;

import com.example.restTemplate.Model.WeatherScore;
import org.springframework.context.ApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


import java.util.Arrays;
import java.util.Random;

@RestController
public class ConsumeWebService {
    @Autowired
    RestTemplate restTemplate;
    WeatherCRUDRepository repository;
    ApplicationContext context;

    @RequestMapping(value = "/weather")
    public void printWeather() {
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        String text = restTemplate.exchange("http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=mykey",
                HttpMethod.GET, entity, String.class).getBody();

        int pressureStartIndex = text.indexOf("pressure")+10;
        int pressureEndIndex = text.indexOf(",", pressureStartIndex);

        int tempStartIndex = text.indexOf("temp")+6;
        int tempEndIndex = text.indexOf(",", tempStartIndex);

        int pressure = Integer.parseInt(text.substring(pressureStartIndex, pressureEndIndex));
        double temperature = Double.parseDouble(text.substring(tempStartIndex, tempEndIndex));

        WeatherScore score = new WeatherScore();
        score.setCity("London");

        score.setPressure(pressure);
        score.setTemperature(temperature);

        Random generator = new Random();
        score.setId(generator.nextInt(1000));

        score.setCurrentDate(System.currentTimeMillis());

        WeatherCRUDRepository repo = context.getBean(WeatherCRUDRepository.class);

        repository.save(score);
    }
}

当我点击 http://localhost:8080/weather 时出现错误:

无法调用“org.springframework.context.ApplicationContext.getBean(java.lang.Class)”,因为“this.context”为空

我怎么能解决这个问题?

标签: mysqlspringspring-mvcspring-data

解决方案


您必须自动装配控制器中的所有 bean,如下所示:

@Autowired
RestTemplate restTemplate;

@Autowired
WeatherCRUDRepository repository;

@Autowired
ApplicationContext context;

推荐阅读