好的做法Spring Boot有错误 - 无法初始化代理

时间:2018-06-15 18:37:26

标签: java rest spring-mvc spring-boot

我是Spring的新手,我正在尝试创建一个学习应用程序,但我在自动装配中遇到问题,我正在添加我的代码。我正在弹簧靴。

我试图从数据库中获取fcm密钥 我不确定这是在组件中获取值的好习惯。

请告知我做错了什么。

错误

2018-06-15 09:59:00.078 ERROR 28776 --- [pool-1-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

org.hibernate.LazyInitializationException: could not initialize proxy [com.mb.push.model.ApiKey#1] - no Session
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:155) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:268) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at com.mb.push.model.ApiKey_$$_jvst1f9_2.getFcmAPIKey(ApiKey_$$_jvst1f9_2.java) ~[classes/:na]
    at com.mb.push.service.FcmpushService.send(FcmpushService.java:37) ~[classes/:na]
    at com.mb.push.scheduled.NotificationsToQueue.sendPushMessage(NotificationsToQueue.java:172) ~[classes/:na]
    at com.mb.push.scheduled.NotificationsToQueue.performCronNewMessages(NotificationsToQueue.java:121) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_172]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_172]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_172]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_172]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_172]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_172]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source) [na:1.8.0_172]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) [na:1.8.0_172]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_172]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_172]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_172]

代码

package com.mb.push.service;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import org.springframework.web.client.RestTemplate;

import com.mb.push.repository.APIKeyRepository;
import com.mb.push.service.HeaderRequestInterceptor;


@Service
public class FcmpushService {

     @Autowired
     private APIKeyRepository apiKeyRepository;





    @Async
    public CompletableFuture<String> send(HttpEntity<String> entity) {

        String  apiKey = apiKeyRepository.getOne(1).getFcmAPIKey();

        System.out.println("------apiKey--------->" + apiKey);
        System.out.println("--------------->");
        System.out.println(entity);
        System.out.println("<---------------");
        RestTemplate restTemplate = new RestTemplate();

        ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new HeaderRequestInterceptor("Authorization", "key=" + apiKey));
        interceptors.add(new HeaderRequestInterceptor("Content-Type", "application/json"));
        restTemplate.setInterceptors(interceptors);

        String firebaseResponse = restTemplate.postForObject(FIREBASE_API_URL, entity, String.class);

        return CompletableFuture.completedFuture(firebaseResponse);
    }
}

0 个答案:

没有答案