【问题标题】:Spring - can't save an entity with CRUD RepositorySpring - 无法使用 CRUD 存储库保存实体
【发布时间】:2021-05-14 09:18:36
【问题描述】:

我正在制作一个简单的程序,它必须从 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”为空 块引用

我该如何解决?

【问题讨论】:

    标签: mysql spring spring-mvc spring-data


    【解决方案1】:

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

    @Autowired
    RestTemplate restTemplate;
    
    @Autowired
    WeatherCRUDRepository repository;
    
    @Autowired
    ApplicationContext context;
    

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2023-03-22
      • 1970-01-01
      • 2017-03-28
      • 2018-01-13
      • 2020-05-05
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多