Home php c# sql c c++ javascript python java go android git linux asp.net django .net node.js ios xcode cocoa iphone mysql tomcat mongodb bash objective-c scala visual-studio apache elasticsearch jar eclipse jquery ruby-on-rails ruby rubygems android-studio spring lua sqlite emacs ubuntu perl docker swift amazon-web-services svn html ajax xml java-ee maven intellij-idea rvm macos unix css ipad postgresql css3 json windows-server vue.js typescript oracle hibernate internet-explorer github tensorflow laravel symfony redis html5 google-app-engine nginx firefox sqlalchemy lucene erlang flask vim solr webview facebook zend-framework virtualenv nosql ide twitter safari flutter bundle phonegap centos Register | Login | Edit Tags | New Questions | 繁体 | 简体


10 questions online user: 17

0
votes
answers
10 views
+10

春季安全:根據服務請求提供訪問

-1

我有春季啓動應用程序,它公開了受春季安全保護的REST端點。春季安全:根據服務請求提供訪問

我需要根據服務調用限制對某些路徑的訪問。比方說,我有一個服務是這樣的:

@Service 
public class AccessService { 
    boolean hasAccess(String requestedPath) { 
     // some business logic here 
    } 
} 

該服務將檢查用戶的角色,一些企業經營狀況,並返回truefalse

現在我需要將此服務調用集成到我的安全配置中。

到目前爲止,我有配置是這樣的:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
     http 
       ... 
       .and().authorizeRequests() 
       .anyRequest().hasRole("USER"); 
} 

我看不出這裏添加服務調用的方式(因爲它是完全靜態的)。

我試圖:

目前我想重寫我的AuthenticationProvider並與附加功能擴展它的。

另一種選擇是將我的REST控制器從一個可以做某種授權的類中擴展出來,但我不確定它是否可行。

問題:如何根據服務方法調用保護REST端點?這樣做的正確方法是什麼?

+1

我建議閱讀[參考指南](https://docs.spring.io/spring-security/site/docs/current/reference/html/el-access.html#el-access-web) 。類似於'anyRequest()。access(「@ accessService.hasAccess(request.requestURI)&& hasRole('USER')」);' –

沙发
0
1

這在the reference guide解釋。基本上你需要使用access表達式而不是hasRole。然後您可以編寫強大的安全表達式。

類似下面應該做的伎倆:

anyRequest() 
    .access("@accessService.hasAccess(request.reques‌?tURI) && hasRole('USER')"); 

這限制與角色ROLE_USER並根據自己的自定義邏輯有機會訪問用戶。

板凳
0
1

我認爲這是一個很好的方法是使用@PreAuthorize 一些文件可以在這裏找到:Expression-Based Access Control
你還可以添加自己的評估類/方法來定製您的特定需求: @PreAuthorize("@customPermissionEvaluator.accessMethod(variable)")

Example類:

@Service(value = "customPermissionEvaluator") 
public class CustomPermissionEvaluatorImpl implements CustomPermissionEvaluator { 
    @Override 
    public boolean accessMethod(int variable) {  
     if (variable == 1) { 
      return true; 
     }  
     return false; 
    } 
} 
0
votes
answers
10 views
+10

如何用Spring Rest Control解決模糊映射?

1

我已經看過以下職位如何用Spring Rest Control解決模糊映射?

1)Error creating bean with name 'requestMappingHandlerAdapter'

2)Spring Boot Ambiguous mapping. Cannot map method

3)Spring mvc Ambiguous mapping found. Cannot map controller bean method

4)Spring MVC Ambiguous mapping. Cannot map

但我一直無法弄清楚如何解決我的問題。我正在創建一個Spring Boot Web應用程序,其中我試圖將以下端點映射到兩個單獨的方法:/quiz/score/{quizId}/quiz/questions/{quizId}端點。

我的功能如下

@RequestMapping(name="/quiz/questions/{quizId}", method=RequestMethod.GET) 
    public ResponseEntity<QuizQuestion> questions(@PathVariable String quizId) { 
    QuizQuestion question = this.quizService.fetchQuestion(quizId); 
    if (question == null) { 
     return new ResponseEntity<QuizQuestion>(HttpStatus.NOT_FOUND); 
    } 
    return new ResponseEntity<QuizQuestion>(question, HttpStatus.OK); 
    } 

@RequestMapping(name="/quiz/score/{id}", method=RequestMethod.GET) 
    public Score getScore(@PathVariable("id") String quizId) { 
    return this.quizService.getScore(quizId); 
    } 

我收到以下錯誤

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/myapplication' method 
public com.project.myapplication.Score com.project.myapplication.QuizController.getScore(java.lang.String) 
to {[],methods=[GET]}: There is already '/myapplication' bean method 
public org.springframework.http.ResponseEntity<com.project.myapplication.QuizQuestion> com.project.myapplication.QuizController.questions(java.lang.String) mapped. 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 

. . . . . . . .. . 

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/myapplication' method 
public com.project.myapplication.Score com.project.myapplication.QuizController.getScore(java.lang.String) 
to {[],methods=[GET]}: There is already '/myapplication' bean method 
public org.springframework.http.ResponseEntity<com.project.myapplication.QuizQuestion> com.project.myapplication.QuizController.questions(java.lang.String) mapped. 
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at 

我知道這兩種方法具有相同的簽名,但他們有兩個獨特的終點。我該如何解決這個問題?

+0

你能顯示我的應用程序端點嗎? – user7294900

+1

無關注意:您的兩個方法實際上不具有相同的簽名,只是相同的參數。對於相同的簽名,他們需要命名相同,但情況並非如此。方法名稱及其參數類型定義方法簽名。 – eis

沙发
0
3

你的問題是,您所指定的終點是這樣的:

@RequestMapping(name="/quiz/score/{id}", method=RequestMethod.GET) 
public Score getScore(@PathVariable("id") String quizId) { 
    return this.quizService.getScore(quizId); 
} 

但他們應該是這樣的:

@RequestMapping(value="/quiz/score/{id}", method=RequestMethod.GET) 
public Score getScore(@PathVariable("id") String quizId) { 
    return this.quizService.getScore(quizId); 
} 

注意而不是

爲了進一步說明,您可以檢查RequestMapping javadoc,它解釋了不同的參數。 name參數只是爲您的映射提供了一個名稱。 value參數是關鍵之一。

0
votes
answers
8 views
+10

在依賴jar中的異步註釋,沒有啓動一個新線程

2

我有一個依賴jar的類,它有一個用Spring @Async註解的方法,我期望這個方法應該在另一個線程中異步執行。在依賴jar中的異步註釋,沒有啓動一個新線程

使用這個類,我在我的父項目中創建一個bean並將它連接到我的類,該類有@Service註釋。在那個類中,我調用了一個有註解的方法,但正如我所說的,該方法不會在另一個線程中執行。

我該如何做到這一點?

這是代碼。

這是我的接口類:

public interface RequestLoggerService { 

    @Async 
    void log(HttpServletRequest request, String serviceName, String requestIdentifier, String message, boolean successful); 
} 

這是實現類:

public class RequestLoggerServiceImpl implements RequestLoggerService { 

    private static Logger logger = Logger.getLogger(RequestLoggerServiceImpl.class); 

    private RequestLoggerDao requestLogger; 

    @Override 
    public void log(HttpServletRequest request, String serviceName, String requestIdentifier, String message, boolean successful) { 
     logRequest(request, serviceName, request.getRequestURI().substring(request.getRequestURI().lastIndexOf("/") + 1), requestIdentifier, message, successful); 
    } 
} 

這兩個類都裝在一個罐子是在我父項目中使用的依賴。我使用這個類來創建父項目一個bean:

<bean id="requestLoggerService" class="com.xyz.service.RequestLoggerServiceImpl"> 
    <property name="requestLogger" ref="requestLoggerDao" /> 
</bean> 

而且這個bean然後自動連線在我@Controller註解類:

public class ActionsController { 

    @Autowired 
    private RequestLoggerService requestLogger; 
} 
+0

代碼必須有問題。發表它。 –

+2

你使用XML配置還是'@ Configuration'/spring boot?如果是後者,那麼您可能需要添加'@ EnableAsync'註釋,請參閱https://spring.io/guides/gs/async-method/請參閱http://stackoverflow.com/questions/31401640/spring-xml - 對等的啓用同步的XML版本 – BretC

+0

嗯,我混了一下,但我有@EnableAsync批註...當我有我的父項目中的異步方法的類,然後一切工作正常,儘快我將它移動到jar中,並從該jar中的類初始化bean,它不起作用... – dplesa

沙发
0
0

我跟着@BretC指令並找到了解決辦法。問題在於我通過XML配置實例化了RequestLoggerService bean,但使用註釋@EnableAsync來啓用@Async註釋發現。

無論如何,我需要使用註解驅動的bean創建,或者在XML配置中啓用異步任務。

0
votes
answers
8 views
+10

如何在Spring Data Rest中爲各種方法添加字段默認值?

0

我想2日期字段添加到我的實體如何在Spring Data Rest中爲各種方法添加字段默認值?

  • creationDate
  • lastModificationDate

我怎麼可以在Spring數據REST爲它們定義的默認值,這樣creationDate得到它的默認值(當前日期)當我創建實體,lastModificationDate獲取它的值作爲該實體的每個放置請求的當前日期?

+0

任何理由不委託給你的數據庫? –

+0

我猜不行,但如果可能的話,這樣我就可以把所有的邏輯放在一個地方 – Carmine

沙发
0
1

嘗試添加到您的模型。

@PrePersist 
void onCreate() { 
    this.setCreationDate(new Timestamp((new Date()).getTime())); 
} 

@PreUpdate 
void onPersist() { 
    this.setLastModificationDate(new Timestamp((new Date()).getTime())); 
} 
板凳
0
1

您可以定義監聽器:

public class EntityListener { 

    @PrePersist 
    public void beforeSave(Entity entity) { 
     entity.setCreationDate(LocalDateTime.now()); 
     entity.setLastModificationDate(LocalDateTime.now()); 
    } 

    @PreUpdate 
    public void beforeUpdate(Entity entity) { 
     entity.setLastModificationDate(LocalDateTime.now()); 
    } 
} 
0
votes
answers
8 views
+10

春天-TX:無法找到春天NamespaceHandler XML模式命名空間[http://www.springframework.org/schema/tx]

0

爲什麼我仍然得到呢?春天-TX:無法找到春天NamespaceHandler XML模式命名空間[http://www.springframework.org/schema/tx]

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [../beans/Character.xml] 
Offending resource: class path resource [spring/config/beanLocations.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx] 
Offending resource: class path resource [spring/beans/Character.xml] 

     at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:72) 

我讀過許多文章中針對此問題,配置爲根據他們我的XML-S。

我的問題的XML看起來像這樣:

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <!-- Character Data Access Object --> 
    <bean id="characterDao" class="com.got.common.dao.CharacterDao" > 
    <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <tx:annotation-driven/> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
</beans> 

春-TX包括在我的pom.xml:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-tx</artifactId> 
    <version>5.0.2.RELEASE</version> 
</dependency> 

beanLocations.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd"> 


     <!-- Database Configuration --> 
     <import resource="../database/DataSource.xml"/> 
     <import resource="../database/hibernate.xml"/> 

     <!-- Beans Declaration --> 
     <import resource="../beans/Character.xml"/> 

</beans> 

組裝插件

<build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <version>3.1.0</version> 
     <executions> 
      <execution> 
      <phase>package</phase> 
      <goals> 
       <goal>single</goal> 
      </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <descriptorRefs> 
      <descriptorRef>jar-with-dependencies</descriptorRef> 
      </descriptorRefs> 
      <archive> 
      <manifest> 
       <addClasspath>true</addClasspath> 
       <classpathPrefix>lib/</classpathPrefix> 
       <mainClass>com.got.common.App</mainClass> 
      </manifest> 
      </archive> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 

我不知道該AOP必須被包括在內,但它的存在。 這個命名空間有什麼問題?我想停止拉我的頭髮,所以請告訴我這個命名空間無法找到的原因。

+0

你證實春天-TX庫實際上是放在你的war文件? – eis

+0

另外,你的'spring/config/beanLocations.xml'中有什麼?某種嵌套定義? – eis

+0

不是,但maven-assembly-plugin應該放在那裏。 (這是一個jar文件。)我在一秒鐘內包含'beanLocations.xml'。 – ntj

沙发
0
1

的問題可能是由您的使用匯編插件,它不明白春天處理機制,並覆蓋在spring.handlers文件處理程序定義造成的。行爲在這個線程中討論:Idea to avoid that spring.handlers/spring.schemas get overwritten when merging multiple spring dependencies in a single jar

爲了避免這個問題,一個選項是使用陰影插件爲您的目的,如建議here

+0

我希望這個答案能解決我的問題,所以我假設你的解決方案可以被接受,謝謝。 – ntj

+1

[此](https://issues.apache.org/jira/browse/MASSEMBLY-360)爲約組裝插件行爲錯誤條目:有趣的部分是,它是權利2010已固定的,測試用例好。看起來似乎並沒有,因爲人們似乎仍然遇到這個問題,所以示例項目證明問題並在那裏發表評論可能會使它在彙編插件(修復程序所屬的地方)中得到修復。他們已經有一個過濾器來處理這個問題,但我想這只是無法正常工作。 – eis

+1

它的工作原理,經過幾個小時的苦難後,無法感謝你足夠的這個鏈接。 – ntj

0
votes
answers
8 views
+10

Spring Boot&Swagger 2:UnsatisfiedDependencyException - 版本庫測試不起作用

1

我有一個Spring Boot Rest API,我一直在工作,上週我添加了Swagger和Swagger-UI。在此之後,庫的測試(僅適用於倉庫相關的測試),開始不工作,當我運行他們,我得到這個錯誤:Spring Boot&Swagger 2:UnsatisfiedDependencyException - 版本庫測試不起作用

java.lang.IllegalStateException: Failed to load ApplicationContext 

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 
    at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
    at org.junit.runners.Suite.runChild(Suite.java:128) 
    at org.junit.runners.Suite.runChild(Suite.java:27) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined in URL [jar:file:/C:/Users/Ale/.m2/repository/io/springfox/springfox-spring-web/2.3.1/springfox-spring-web-2.3.1.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/C:/Users/Ale/.m2/repository/io/springfox/springfox-spring-web/2.3.1/springfox-spring-web-2.3.1.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) 
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) 
    ... 33 more 
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/C:/Users/Ale/.m2/repository/io/springfox/springfox-spring-web/2.3.1/springfox-spring-web-2.3.1.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) 
    ... 51 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) 
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) 
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) 
    ... 65 more 

我不知道爲什麼這個錯誤發生的事情,因爲這些測試與Swagger無關。

的配置代碼是這樣的:

@EnableSwagger2 
@SpringBootApplication 
public class UserServiceApplication extends WebMvcConfigurerAdapter { 

    public static void main(String[] args) { 
     SpringApplication.run(UserServiceApplication.class, args); 
    } 

    @Bean 
    public Docket productApi() { 
     return new Docket(DocumentationType.SWAGGER_2) 
       .select().apis(RequestHandlerSelectors.basePackage(getClass().getPackage().getName())) 
       .paths(PathSelectors.any()) 
       .build() 
       .apiInfo(generateApiInfo()); 
    } 

    private ApiInfo generateApiInfo() { 
     return new ApiInfo("User Service", "User Service", "", "", "", "", ""); 
    } 
} 

的POM文件是這樣的,有揚鞭2.3.1:

<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger2</artifactId> 
    <version>${io.springfox.springfox-swagger2}</version> 
</dependency> 

<!-- Include swagger for API description UI --> 
<dependency> 
    <groupId>io.springfox</groupId> 
    <artifactId>springfox-swagger-ui</artifactId> 
    <version>${io.springfox.springfox-swagger-ui}</version> 
</dependency> 

而且測試非常簡單:

@RunWith(SpringRunner.class) 
@DataJpaTest 
@Transactional 
public class UserRepositoryTest { 

    @Autowired 
    private TestEntityManager entityManager; 

    @Autowired 
    private UserRepository userRepository; 

    @Test 
    public void whenPersistingAnUserTheUserAndImageAreSaved(){ 
     UserImage userImage = new UserImage("key","URL"); 
     User user = new User("[email protected]", "pass","name","lastNmae","description","123123",userImage); 
     User savedUser = userRepository.save(user); 
     assertNotNull(savedUser.getId()); 
     assertNotNull(savedUser.getUserImage().getId()); 
    } 

你能幫我嗎?其餘的測試正常工作。 謝謝!

+0

@WebAppConfiguration上的測試? – MarianP

+0

@MarianP你的意思是我必須把註釋放在測試類中嗎?爲什麼?我正在測試版本庫,而不是控制器。謝謝。 – AleGallagher

沙发
0
5

問題是您的中央配置類UserServiceApplication設置了Swagger和Spring MVC。當你使用@DataJpaTest時,你有效地告訴Spring Boot 而不是加載Spring MVC相關的基礎結構。但是......由@EnableSwagger2創建的一些bean期望MVC基礎架構到位,@DataJpaTest不是這種情況。

因此,爲了從春天啓動的測試切片件(即,@DataJpaTest),你需要招搖和MVC配置移動到一個單獨的@Configuration類受益。

+0

非常感謝你@Sam Brannen!它正在工作:) – AleGallagher

+0

你能分享你的解決方案嗎? –

0
votes
answers
8 views
+10

Spring狀態機JPA持久性

0

我是Spring新狀態機我有下面給出的狀態配置我需要在mysql中使用JPA持久化狀態更改。任何適當的例子也對我非常有幫助。在此先感謝Spring狀態機JPA持久性

@Configuration 
@EnableStateMachine(name = "machine1") 
public class Config extends StateMachineConfigurerAdapter<String, String>{ 

@Override 
public void configure(StateMachineConfigurationConfigurer<String, String> config) throws Exception { 
    config.withConfiguration().autoStartup(true).listener(listener()); 
} 

@Override 
public void configure(StateMachineStateConfigurer<String, String> states) throws Exception { 
    states 
     .withStates() 
      .initial("S1") 
      .state("S1") 
      .state("S2",null,action1()) 
      .state("S3"); 
} 

@Override 
public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception { 
    transitions 
     .withExternal() 
      .source("S1") 
      .target("S2") 
      .event("E1") 
      .and().withExternal() 
      .source("S2") 
      .target("S3") 
      .event("E2"); 
} 

} 
沙发
0
1

jpa-config只是一個例子在一個數據庫中保持機器配置(狀態,轉換等)。如果您使用其他方式(javadsl或uml)進行配置,則不需要此操作。這種支持正在增加,因爲有些人希望有一種方法來修改機器配置,而無需再次編譯源代碼。我目前正在努力通過相同類型的彈簧數據存儲庫抽象爲持久化機器添加更好的支持,這應該在1.2.8版本中着陸。

其他一些示例是一些示例,可以如何手動完成。目前這個過程確實是非常手動,低水平,相當麻煩。如果你不急於使用,我建議使用1.2.x分支的1.2.8快照。即有新的樣本datajpapersist在運行時顯示更清潔的模型持久機器。

0
votes
answers
8 views
+10

試圖重現彈簧安全OAuth2Login示例

0

我想學習一些Spring Security時不斷出現錯誤。但是我有麻煩來通過Auth2/OpenId Connect來保護我的網頁。我嘗試重現Spring Security Reference OAuth2 Client的示例。試圖重現彈簧安全OAuth2Login示例

但我已經堅持在開始運行該項目。我使用maven而不是gradle作爲構建工具。 如果我運行從例子中的測試中,我不斷收到一個例外,它說的是,應用程序上下文無法加載

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available: expected at least 1 bean which qualifies as autowire candidate. 

完整的異常

java.lang.IllegalStateException: Failed to load ApplicationContext 
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12] 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12] 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12] 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12] 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12] 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12] 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12] 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na] 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na] 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na] 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) [.cp/:na] 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na] 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na] 
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'OAuth2LoginApplicationTests.SpringBootApplicationTestConfig': Unsatisfied dependency expressed through field 'clientRegistrationRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] 
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE] 
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120) ~[spring-boot-test-1.5.8.RELEASE.jar:1.5.8.RELEASE] 
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
... 25 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
... 43 common frames omitted 

然後即時得到一個奇怪的編譯使用示例中完全相同的代碼時出現錯誤。

即使在HttpSecuritydocumentation中,該方法似乎也不存在。我對這個問題非常困惑。我是否導入錯誤的依賴關係?

OAuth2LoginApplicationTests.java線356?386

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .anyRequest().authenticated() 
       .and() 
      .oauth2Login()  <-- this method does not exist 
       .tokenEndpoint() 
        .accessTokenResponseClient(this.mockAccessTokenResponseClient()) 
        .and() 
       .userInfoEndpoint() 
        .userService(this.mockUserService()); 
    } 

的pom.xml依賴

<repositories> 
    <repository> 
     <id>spring-milestones</id> 
     <name>Spring Milestones</name> 
     <url>https://repo.spring.io/libs-milestone</url> 
     <snapshots> 
      <enabled>false</enabled> 
     </snapshots> 
    </repository> 
</repositories> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-web</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-config</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-test</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <!-- OAuth2 for clients --> 
    <dependency> 
     <groupId>org.springframework.security.oauth</groupId> 
     <artifactId>spring-security-oauth2</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-oauth2-client</artifactId> 
     <version>5.0.0.RC1</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-oauth2-jose</artifactId> 
     <version>5.0.0.RC1</version> 
    </dependency> 

    <dependency> 
     <groupId>net.sourceforge.htmlunit</groupId> 
     <artifactId>htmlunit</artifactId> 
     <scope>test</scope> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webflux --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webflux</artifactId> 
     <version>5.0.1.RELEASE</version> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 --> 
    <dependency> 
     <groupId>org.thymeleaf.extras</groupId> 
     <artifactId>thymeleaf-extras-springsecurity4</artifactId> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 

    <!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core --> 
    <dependency> 
     <groupId>io.projectreactor</groupId> 
     <artifactId>reactor-core</artifactId> 
     <version>3.1.2.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>io.projectreactor.ipc</groupId> 
     <artifactId>reactor-netty</artifactId> 
     <version>0.7.2.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>com.jayway.jsonpath</groupId> 
     <artifactId>json-path</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.projectlombok</groupId> 
     <artifactId>lombok</artifactId> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>com.google.code.findbugs</groupId> 
     <artifactId>jsr305</artifactId> 
     <version>3.0.1</version> 
     <scope>provided</scope> 
    </dependency> 
</dependencies> 

我是否錯過任何依賴關係或有錯誤的版本?如果我用gradle導入完整的項目,一切正常。

沙发
0
0

我發現了爲什麼會出現異常。我使用了錯誤版本作爲spring boot parent。

的例子使用的春天啓動的里程碑版本2.0.0.M6而不是最新發布版本1.5.8.RELEASE

我只是不得不改變

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.8.RELEASE</version> 
</parent> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>2.0.0.M6</version> 
</parent> 

注意塔t這是不是的發佈版!

0
votes
answers
8 views
+10

Log4j2 JPAAppender BLOB

0

後2天log4j2戰鬥,Hibernate和Spring我終於JPAAppender工作..排序的..Log4j2 JPAAppender BLOB

我跟着這個例子:http://logging.apache.org/log4j/2.x/manual/appenders.html#JPAAppender

例子中的類創建一個表有這個結構:

CREATE TABLE `applicationLog` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT, 
    `wrappedEvent` tinyblob, 
    PRIMARY KEY (`id`) 
) 

第一個問題是我不能記錄任何東西,因爲tinyblob太小了。我通過將tinyblob更改爲largeblob來手動解決此問題。

第一個問題:有沒有辦法讓類擴展BasicLogEventEntity來自己創建一個largeblob?

第二個問題:如何使用mysql客戶端讀取wrappedEvent?如何從日誌中獲取日誌的純文本?

感謝

沙发
0
0

所以我改變了實體如下,現在在db斑點總是空的(我並不需要它了),在其他列逐漸加入可讀的信息。這只是一個解決方法,但它是我能想到的最好的...

import java.time.Instant; 
import java.time.LocalDateTime; 
import java.time.ZoneId; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 
import lombok.Getter; 
import lombok.Setter; 
import org.apache.commons.lang3.exception.ExceptionUtils; 
import org.apache.logging.log4j.core.LogEvent; 
import org.apache.logging.log4j.core.appender.db.jpa.BasicLogEventEntity; 
import org.hibernate.annotations.Type; 

/** 
* 
* @author syco 
*/ 
@Entity(name = "applicationLog") 
@Table(name = "`applicationLog`") 
public class ApplicationLog extends BasicLogEventEntity { 

    @Getter 
    @Setter 
    @Column(name = "`customEventDate`") 
    private LocalDateTime customEventDate; 

    @Getter 
    @Setter 
    @Column(name = "`customException`") 
    @Type(type = "text") 
    private String customException; 

    @Getter 
    @Setter 
    @Column(name = "`customLevel`", length = 512) 
    private String customLevel; 

    @Getter 
    @Setter 
    @Column(name = "`customLogger`", length = 512) 
    private String customLogger; 

    @Getter 
    @Setter 
    @Column(name = "`customMessage`") 
    @Type(type = "text") 
    private String customMessage; 

    @Getter 
    @Setter 
    @Column(name = "`customThreadName`", length = 512) 
    private String customThreadName; 

    @Getter 
    @Setter 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "`id`") 
    private long id; 

    public ApplicationLog() { 
    super(); 
    } 

    public ApplicationLog(LogEvent logEvent) { 
    super(); 
    if (logEvent != null) { 
     setCustomEventDate(Instant.ofEpochMilli(logEvent.getTimeMillis()).atZone(ZoneId.systemDefault()).toLocalDateTime()); 
     if (logEvent.getThrown() != null) { 
     setCustomException(ExceptionUtils.getStackTrace(logEvent.getThrown())); 
     } 
     if (logEvent.getLevel() != null) { 
     setCustomLevel(logEvent.getLevel().name()); 
     } 
     setCustomLogger(logEvent.getLoggerName()); 
     if (logEvent.getMessage() != null) { 
     setCustomMessage(logEvent.getMessage().toString()); 
     } 
     setCustomThreadName(logEvent.getThreadName()); 
    } 
    } 
} 
0
votes
answers
7 views
+10

使用io.netty創建Maven的問題:netty-handler:jar:4.1.17.Final

0

任何人都可以在解決Maven應用程序的問題上長時間苦苦掙扎。 我正在使用spring引導應用程序連接到cassandra。每當我們做一個MVN清潔套裝= -DskipTests真實,我們得到以下警告: -使用io.netty創建Maven的問題:netty-handler:jar:4.1.17.Final

Clean Building screenshot

和上運行的應用程序失敗(Java的罐子目標/ DemoSearch-0.0.1-SNAPSHOT.jar)和附帶的是堆棧跟蹤。

> Unsatisfied dependency expressed through constructor parameter 2; 
> nested exception is 
> org.springframework.beans.factory.BeanCreationException: Error 
> creating bean with name 'cassandraCluster' defined in class path 
> resource [org/springframework/boot/autoc 
> onfigure/cassandra/CassandraAutoConfiguration.class]: Bean 
> instantiation via factory method failed; nested exception is 
> org.springframework.beans.BeanInstantiationExcept ion: Failed to 
> instantiate [com.datastax.driver.core.Cluster]: Factory method 
> 'cassandraCluster' threw exception; nested exception is 
> java.lang.NoClassDefFoundError: io/ netty/util/Timer 
>   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0 .2.RELEASE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5 .0.2.RELEASE] 
>   ... 40 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error 
> creating bean with name 'persistentEntities' defined in class path 
> resource [org/springframewor 
> k/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean 
> instantiation via factory method failed; nested exception is 
> org.springframework.beans.BeanInstanti ationException: Failed to 
> instantiate 
> [org.springframework.data.mapping.context.PersistentEntities]: Factory 
> method 'persistentEntities' threw exception; nested exceptio n is 
> org.springframework.beans.factory.UnsatisfiedDependencyException: 
> Error creating bean with name 
> 'org.springframework.boot.autoconfigure.data.cassandra.CassandraData 
> AutoConfiguration': Unsatisfied dependency expressed through 
> constructor parameter 2; nested exception is 
> org.springframework.beans.factory.BeanCreationException: Error 
> creating bean with name 'cassandraCluster' defined in class path 
> resource 
> [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: 
> Bean insta ntiation via factory method failed; nested exception is 
> org.springframework.beans.BeanInstantiationException: Failed to 
> instantiate [com.datastax.driver.core.Cluster]: F actory method 
> 'cassandraCluster' threw exception; nested exception is 
> java.lang.NoClassDefFoundError: io/netty/util/Timer 
>   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5 .0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) 
> ~[spr ing-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) 
> ~[spring-beans-5 .0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
> ~[spring-beans-5.0.2.RE LEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 
> ~[spring-beans-5.0.2.RELE ASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEAS E] 
>   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
> ~[spring-beans-5.0.2.RELEASE.jar!/: 
> 5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.resolveBeanReference(ConfigurationClassEnhancer.java:392) 
> ~[spring-con text-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:364) 
> ~[spring-context-5.0.2. RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b.persistentEntities(<generated>) 
> ~[spring-data-rest 
> -webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.jsonSchemaConverter(RepositoryRestMvcConfiguration.java:426) 
> ~[spring-data-rest-web mvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b.CGLIB$jsonSchemaConverter$26(<generated>) 
> ~[spring 
> -data-rest-webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b$$FastClassBySpringCGLIB$$553cc2af.invoke(<generate 
> d>) ~[spring-data-rest-webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
> ~[spring-core-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) 
> ~[spring-context-5.0.2. RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b.jsonSchemaConverter(<generated>) 
> ~[spring-data-res t-webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 
>   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0 .2.RELEASE] 
>   ... 41 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to 
> instantiate 
> [org.springframework.data.mapping.context.PersistentEntities]: Factory 
> method 'per sistentEntities' threw exception; nested exception is 
> org.springframework.beans.factory.UnsatisfiedDependencyException: 
> Error creating bean with name 'org.springframewor 
> k.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration': 
> Unsatisfied dependency expressed through constructor parameter 2; 
> nested exception is org.springfram 
> ework.beans.factory.BeanCreationException: Error creating bean with 
> name 'cassandraCluster' defined in class path resource 
> [org/springframework/boot/autoconfigure/cassan 
> dra/CassandraAutoConfiguration.class]: Bean instantiation via factory 
> method failed; nested exception is 
> org.springframework.beans.BeanInstantiationException: Failed to 
> instantiate [com.datastax.driver.core.Cluster]: Factory method 
> 'cassandraCluster' threw exception; nested exception is 
> java.lang.NoClassDefFoundError: io/netty/util/Time r 
>   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0 .2.RELEASE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5 .0.2.RELEASE] 
>   ... 63 common frames omitted Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 
> Error creating bean with name 
> 'org.springframework.boot.autoconfigure.data.cassandra.Cassand 
> raDataAutoConfiguration': Unsatisfied dependency expressed through 
> constructor parameter 2; nested exception is 
> org.springframework.beans.factory.BeanCreationException: Error 
> creating bean with name 'cassandraCluster' defined in class path 
> resource 
> [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: 
> Bean instantiation via factory method failed; nested exception is 
> org.springframework.beans.BeanInstantiationException: Failed to 
> instantiate [com.datastax.driver.core.Clust er]: Factory method 
> 'cassandraCluster' threw exception; nested exception is 
> java.lang.NoClassDefFoundError: io/netty/util/Timer 
>   at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEA SE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEA SE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270) 
> ~[spring-beans- 
> 5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127) 
> ~[spring-beans-5 .0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
> ~[spring-beans-5.0.2.RE LEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 
> ~[spring-beans-5.0.2.RELE ASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEAS E] 
>   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
> ~[spring-beans-5.0.2.RELEASE.jar!/: 
> 5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5 .0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) 
> ~[spr ing-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) 
> ~[spring-beans-5 .0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
> ~[spring-beans-5.0.2.RE LEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 
> ~[spring-beans-5.0.2.RELE ASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEAS E] 
>   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
> ~[spring-beans-5.0.2.RELEASE.jar!/: 
> 5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:516) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5. 
> 0.2.RELEASE] 
>   at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:504) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5. 
> 0.2.RELEASE] 
>   at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1194) 
> ~[spring-context-5.0.2.RELEASE.jar!/:5.0.2 .RELEASE] 
>   at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:263) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.persistentEntities(RepositoryRestMvcConfiguration.java:236) 
> ~[spring-data-rest-webm vc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b.CGLIB$persistentEntities$19(<generated>) 
> ~[spring- data-rest-webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b$$FastClassBySpringCGLIB$$553cc2af.invoke(<generate 
> d>) ~[spring-data-rest-webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
> ~[spring-core-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) 
> ~[spring-context-5.0.2. RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration$$EnhancerBySpringCGLIB$$4669ff6b.persistentEntities(<generated>) 
> ~[spring-data-rest 
> -webmvc-3.0.2.RELEASE.jar!/:3.0.2.RELEASE] 
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 
>   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0 .2.RELEASE] 
>   ... 64 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error 
> creating bean with name 'cassandraCluster' defined in class path 
> resource [org/springframework/ 
> boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]: Bean 
> instantiation via factory method failed; nested exception is 
> org.springframework.beans.BeanInstantia tionException: Failed to 
> instantiate [com.datastax.driver.core.Cluster]: Factory method 
> 'cassandraCluster' threw exception; nested exception is 
> java.lang.NoClassDefFound Error: io/netty/util/Timer 
>   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5 .0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) 
> ~[spr ing-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) 
> ~[spring-beans-5 .0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
> ~[spring-beans-5.0.2.RE LEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) 
> ~[spring-beans-5.0.2.RELE ASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEAS E] 
>   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
> ~[spring-beans-5.0.2.RELEASE.jar!/: 
> 5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEASE ] 
>   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1135) 
> ~[spring-beans-5.0.2.RELEASE.ja r!/:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062) 
> ~[spring-beans-5.0.2.RELEASE.jar! /:5.0.2.RELEASE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2. RELEASE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:721) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0.2.RELEA SE] 
>   ... 97 common frames omitted Caused by: org.springframework.beans.BeanInstantiationException: Failed to 
> instantiate [com.datastax.driver.core.Cluster]: Factory method 
> 'cassandraCluster' threw except ion; nested exception is 
> java.lang.NoClassDefFoundError: io/netty/util/Timer 
>   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0 .2.RELEASE] 
>   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5 .0.2.RELEASE] 
>   ... 110 common frames omitted Caused by: java.lang.NoClassDefFoundError: io/netty/util/Timer 
>   at com.datastax.driver.core.Configuration$Builder.build(Configuration.java:317) 
> ~[cassandra-driver-core-3.3.2.jar!/:na] 
>   at com.datastax.driver.core.Cluster$Builder.getConfiguration(Cluster.java:1318) 
> ~[cassandra-driver-core-3.3.2.jar!/:na] 
>   at com.datastax.driver.core.Cluster.<init>(Cluster.java:113) ~[cassandra-driver-core-3.3.2.jar!/:na] 
>   at com.datastax.driver.core.Cluster.buildFrom(Cluster.java:178) 
> ~[cassandra-driver-core-3.3.2.jar!/:na] 
>   at com.datastax.driver.core.Cluster$Builder.build(Cluster.java:1335) 
> ~[cassandra-driver-core-3.3.2.jar!/:na] 
>   at org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration.cassandraCluster(CassandraAutoConfiguration.java:86) 
> ~[spring-boot-autoconfigure-2 
> .0.0.BUILD-SNAPSHOT.jar!/:2.0.0.BUILD-SNAPSHOT] 
>   at org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration$$EnhancerBySpringCGLIB$$26c07881.CGLIB$cassandraCluster$0(<generated>) 
> ~[spring-bo 
> ot-autoconfigure-2.0.0.BUILD-SNAPSHOT.jar!/:2.0.0.BUILD-SNAPSHOT] 
>   at org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration$$EnhancerBySpringCGLIB$$26c07881$$FastClassBySpringCGLIB$$f566d5b3.invoke(<generat 
> ed>) 
> ~[spring-boot-autoconfigure-2.0.0.BUILD-SNAPSHOT.jar!/:2.0.0.BUILD-SNAPSHOT] 
>   at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
> ~[spring-core-5.0.2.RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) 
> ~[spring-context-5.0.2. RELEASE.jar!/:5.0.2.RELEASE] 
>   at org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration$$EnhancerBySpringCGLIB$$26c07881.cassandraCluster(<generated>) 
> ~[spring-boot-autoc 
> onfigure-2.0.0.BUILD-SNAPSHOT.jar!/:2.0.0.BUILD-SNAPSHOT] 
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101] 
>   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_101] 
>   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) 
> ~[spring-beans-5.0.2.RELEASE.jar!/:5.0 .2.RELEASE] 
>   ... 111 common frames omitted Caused by: java.lang.ClassNotFoundException: io.netty.util.Timer 
>   at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_101] 
>   at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_101] 
>   at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93) 
> ~[DemoSearch-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT] 
>   at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_101] 
>   ... 127 common frames omitted 
+0

看起來像用於開發casandra的彈簧數據數據的彈簧引導框架與您擁有的彈簧引導罐不匹配。請使用適當的版本。 com.datastax.cassandra 卡桑德拉驅動核心 3.3.1 春季啓動版本1.5.8.RELEASE –

沙发
0
0

以下是pom.xml中: -

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.demo.search</groupId> 
    <artifactId>DemoSearch</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>DemoSearch</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>2.0.0.BUILD-SNAPSHOT</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-cassandra</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-rest</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-solr</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jersey</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <!-- <dependency> 
      <groupId>commons-lang</groupId> 
      <artifactId>commons-lang</artifactId> 
      <version>2.6</version> 
     </dependency> --> 
    <!-- https://mvnrepository.com/artifact/io.netty/netty-all --> 
<!-- <dependency> 
    <groupId>io.netty</groupId> 
    <artifactId>netty-all</artifactId> 
    <version>4.0.9.Final</version> 
</dependency>--> 
<dependency> 
    <groupId>com.google.guava</groupId> 
    <artifactId>guava</artifactId> 
    <version>16.0.1</version> 
</dependency> 

</dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 

    <repositories> 
     <!-- <repository> 
      <id>spring-snapshots</id> 
      <name>Spring Snapshots</name> 
      <url>https://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </repository> --> 
     <repository> 
      <id>spring-milestones</id> 
      <name>Spring Milestones</name> 
      <url>https://repo.spring.io/milestone</url> 
      <snapshots> 
       <enabled>false</enabled> 
      </snapshots> 
     </repository> 
     <repository> 
     <id>spring-snapshots</id> 
     <name>Spring Snapshots</name> 
     <url>https://repo.spring.io/libs-snapshot</url> 
     <snapshots> 
      <enabled>true</enabled> 
     </snapshots> 
    </repository> 
    </repositories> 
    <pluginRepositories> 
     <pluginRepository> 
      <id>spring-snapshots</id> 
      <name>Spring Snapshots</name> 
      <url>https://repo.spring.io/snapshot</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </pluginRepository> 
     <pluginRepository> 
      <id>spring-milestones</id> 
      <name>Spring Milestones</name> 
      <url>https://repo.spring.io/milestone</url> 
      <snapshots> 
       <enabled>true</enabled> 
      </snapshots> 
     </pluginRepository> 
    </pluginRepositories> 
</project> 
板凳
0
0

你得到transitive dependency警告,因爲您要添加netty-all jar版本,這是從春天啓動加罐子版本不同。如果您從依賴項中刪除版本號,那麼您將不會收到該警告。我沒有你的Cassandra相關文件和你的Spring啓動文件,所以我不能看到我是否得到ClassNotFoundException。但我用另一個類 - 從io.netty.util包,我可以加載該類沒有任何錯誤。