mysql - 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”为空
我怎么能解决这个问题?
解决方案
您必须自动装配控制器中的所有 bean,如下所示:
@Autowired
RestTemplate restTemplate;
@Autowired
WeatherCRUDRepository repository;
@Autowired
ApplicationContext context;
推荐阅读
- keycloak - Red Hat SSO 7.3.0.GA 的 Keycloak API 调用依赖项
- http - 戈朗。如何自动授权我的应用访问 spotify API?
- java - Intellij 中的 AWSToolkit,环境变量不起作用
- javascript - 使用 Jest 对仅在函数存在时调用函数的 JavaScript 进行单元测试
- python - 无法摆脱使用 asyncio 时出现的一些错误页面
- php - 使用 htaccess 重定向到 https,但不使用 XAMPP 进行开发
- c# - 我可以对 Azure Cosmos 中的 DateTimeOffset 序列化对象使用 DateTimePart 函数吗?
- c# - 无法将 Json 字符串反序列化为 .NET 对象,但反序列化后对象为空
- c# - 使用 Nodejs/Electron 处理 Windows COM 对象
- c++ - 如何获取特定子进程的进程 ID?(C++)