我使用入门选择Java Spring Basic创建了一个Web应用程序。我的项目包含树服务:语言翻译器,MongoDb,推送通知。
我使用Spring技术重写了我的代码,我的maven安装失败了。
显示错误:
org.springframework.beans.BeanInstantiationException:失败 实例 [com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator]: 工厂方法' languageTranslator'抛出异常;嵌套异常 是java.lang.NoClassDefFoundError:okhttp3 / RequestBody
我尝试使用命令行连接到我的语言翻译服务,它工作正常。
package application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SBApplication {
public static void main(String[] args) {
SpringApplication.run(SBApplication.class, args);
}
}
我的配置类,用于实例化我的语言翻译服务
package application;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator;
@Configuration
@ConditionalOnClass(LanguageTranslator.class)
public class LanguageTranslatorServiceConfig {
@Value("${languagetranslator.username}")
String username;
@Value("${languagetranslator.password}")
String password;
@Bean
@ConditionalOnMissingBean
public LanguageTranslator languageTranslator() {
LanguageTranslator language = new LanguageTranslator();
language.setUsernameAndPassword(username, password);
return language;
}
}
我也试过没有@ConditionalOnClass(LanguageTranslator.class)和@ConditionalOnMissingBean注释。 languagetranslator.username在application.properties文件中声明。
的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>projects</groupId>
<artifactId>LanguageTranslatorApi</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
<app.name>LanguageTranslatorApi</app.name>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- ADD -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.mobilefirstplatform.serversdk.java</groupId>
<artifactId>push</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
</dependency>
<!-- Spring connector -->
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-localconfig-connector</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
</dependency> -->
<!-- WATSON SERVICES -->
<dependency>
<groupId>com.ibm.watson.developer_cloud</groupId>
<artifactId>java-sdk</artifactId>
<version>4.0.0</version>
</dependency>
<!-- PUSH NOTIFICATION -->
<dependency>
<groupId>com.ibm.mobilefirstplatform.serversdk.java</groupId>
<artifactId>push</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的服务名称正确写在 manifest.yml
中---
applications:
- instances: 1
timeout: 180
name: LanguageTranslatorApi
buildpack: java_buildpack
path: ./target/LanguageTranslatorApi-1.0-SNAPSHOT.jar
memory: 256M
domain: mybluemix.net
host: languagetranslator
services:
- languagetranslatorapi-imfpush-1511857545092
- languagetranslatorapi-language-t-1511857521703
列出错误:
引起:org.springframework.beans.BeanInstantiationException: 无法实例化 [com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator]: 工厂方法&#39; languageTranslator&#39;抛出异常;嵌套异常 是java.lang.NoClassDefFoundError:okhttp3 / RequestBody
引起:org.springframework.beans.factory.BeanCreationException: 创建名称为&#39; languageTranslator&#39;的bean时出错在课堂上定义 路径资源[application / LanguageTranslatorServiceConfig.class]: 通过工厂方法进行Bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例 [com.ibm.watson.developer_cloud.language_translator.v2.LanguageTranslator]: 工厂方法&#39; languageTranslator&#39;抛出异常;嵌套异常 是java.lang.NoClassDefFoundError:okhttp3 / RequestBody
我的项目结构:控制器 - &gt; Ucc - &gt; LanguageTranslatorService
package application.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import application.business.Translator;
import application.business.dto.TranslatorDto;
import application.ucc.LanguageTranslatorUcc;
@RestController
public class LanguageController {
private LanguageTranslatorUcc languageTranslatorUcc;
@Autowired
public LanguageController(LanguageTranslatorUcc languageTranslatorUcc) {
this.languageTranslatorUcc = languageTranslatorUcc;
}
@RequestMapping(method=RequestMethod.POST, value="/translate")
public Translator translation(@RequestBody TranslatorDto translator) {
System.out.println(translator.toString());
return languageTranslatorUcc.translator(translator);
}
}
package application.ucc.imp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import application.business.Translator;
import application.business.dto.TranslatorDto;
import application.service.LanguageTranslatorService;
import application.ucc.LanguageTranslatorUcc;
@Service
public class LanguageTranslatorUccImpl implements LanguageTranslatorUcc{
private LanguageTranslatorService languageTranslatorService;
@Autowired
public LanguageTranslatorUccImpl(LanguageTranslatorService languageTranslatorService) {
this.languageTranslatorService = languageTranslatorService;
}
@Override
public Translator translator(TranslatorDto translator) {
return languageTranslatorService.translation("en-fr", translator);
}
}
@Service
public class LanguageTranslatorServiceImpl implements LanguageTranslatorService{
private LanguageTranslator languageTranslator;
@Autowired
public LanguageTranslatorServiceImpl(LanguageTranslator languageTranslator) {
this.languageTranslator = languageTranslator;
}
@Override
public Translator translation(String idModel, TranslatorDto translator) {
// TODO Auto-generated method stub
return null;
}
}
我该如何解决这个问题?