我正计划处理一些交易数据,并请求检索一些我希望存储在mysql中的市场数据。
从昨天起,我就被困在一个 NullPointerException
在我的密码里。在stackoverflow上,我经常看到尝试示例化服务的错误,例如忘记注解。
对我来说,每当我尝试使用自动连线服务时,它似乎都会出错。我现在从main方法调用这个方法(在控制器中发出数据请求)。这意味着我必须示例化才能到达那里。这可能就是问题所在。我可能缺少如何正确处理这个问题或如何将数据保存到dao的概念。希望有人能把我引向正确的方向。
请参见下面的代码:
@SpringBootApplication
public class DesktopAutoTradingApplication {
public static void main(String[] args) throws JsonProcessingException {
SpringApplication.run(DesktopAutoTradingApplication.class, args);
MarketsBinanceController marketsBinanceController = new MarketsBinanceController();
marketsBinanceController.saveListOfMarketsBinance();
}
@Controller
public class MarketsBinanceController{
@Autowired
RequestServices requestService;
public void saveListOfMarketsBinance() throws JsonProcessingException {
String resourceURL;
RestTemplate restTemplate = new RestTemplate();
ObjectMapper mapper = new ObjectMapper();
ResponseEntity<String> response;
MarketsBinance marketsBinance;
resourceURL = "https://api.cryptowat.ch/markets/binance";
response = restTemplate.getForEntity(resourceURL, String.class);
JsonNode root = mapper.readTree(response.getBody());
JsonNode result = root.get("result");
System.out.println(result);
List<MarketsBinance> markets = new ArrayList<>();
for(JsonNode item : result){
marketsBinance = new MarketsBinance();
MarketsBinanceDto marketsBinanceDto;
marketsBinanceDto = mapper.treeToValue(item,MarketsBinanceDto.class);
marketsBinance.setId(marketsBinanceDto.getId());
marketsBinance.setExchange(marketsBinanceDto.getExchange());
marketsBinance.setPair(marketsBinanceDto.getPair());
marketsBinance.setActive(marketsBinanceDto.getActive());
marketsBinance.setRoute(marketsBinanceDto.getRoute());
markets.add(marketsBinance);
requestService.saveMarketsBinance(markets);
}
}
}
@Service
@Transactional
public class RequestServices {
@Autowired
private MarketsBinanceDAO marketsBinanceDAO;
public void saveMarketsBinance(List<MarketsBinance> markets){
marketsBinanceDAO.saveAll(markets);
}
}
@Component
public interface MarketsBinanceDAO extends CrudRepository<MarketsBinance, Long> {
}
@Entity
public class MarketsBinance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long tableId;
private Long id;
private String exchange;
private String pair;
private Boolean active;
private String route;
//getters and setters
public class MarketsBinanceDto {
private Long id;
private String exchange;
private String pair;
private Boolean active;
private String route;
//getters and setters
Exception in thread "main" java.lang.NullPointerException
at Controller.MarketsBinanceController.saveListOfMarketsBinance(MarketsBinanceController.java:46)
at com.DAT.DesktopAutoTrading.DesktopAutoTradingApplication.main(DesktopAutoTradingApplication.java:16)
下面是运行程序时收到的错误消息,在第一个答案中添加了建议的解决方案。
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'Controller.MarketsBinanceController' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:352)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127)
at com.DAT.DesktopAutoTrading.DesktopAutoTradingApplication.main(DesktopAutoTradingApplication.java:15)
2020-09-07 12:24:03.545 INFO 22756 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-09-07 12:24:03.553 INFO 22756 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
1条答案
按热度按时间fnatzsnv1#
你应该使用类型的bean
MarketsBinanceController
而不是MarketsBinanceController marketsBinanceController = new MarketsBinanceController();
.要在main方法中获得这个bean,您应该
ApplicationContext
.ApplicationContext
您可以作为返回对象从SpringApplication.run(DesktopAutoTradingApplication.class, args);
.完整代码示例: