我是Java和Spring的新手。 我尝试从https://spring.io/guides/gs/producing-web-service/开始 我将Gradle bulidship 2.0的inital和complet解决方案的内容下载到了STS 3.9.5 IDE中。但是设置此项目尚无法使用。 在 hello 包中,导入似乎不好。
import io.spring.guides.gs_production_web_service.GetCountryRequest; 导入io.spring.guides.gs_production_web_service.GetCountryResponse;
我收到消息 “无法解析导入io”
我该如何解决?
我没有更改bulid.gradle中的任何内容:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
}
// tag::xsd[]
task genJaxb {
ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
ext.classesDir = "${buildDir}/classes/jaxb"
ext.schema = "src/main/resources/countries.xsd"
outputs.dir classesDir
doLast() {
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
mkdir(dir: sourcesDir)
mkdir(dir: classesDir)
xjc(destdir: sourcesDir, schema: schema) {
arg(value: "-wsdl")
produces(dir: sourcesDir, includes: "**/*.java")
}
javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true,
debugLevel: "lines,vars,source",
classpath: configurations.jaxb.asPath) {
src(path: sourcesDir)
include(name: "**/*.java")
include(name: "*.java")
}
copy(todir: classesDir) {
fileset(dir: sourcesDir, erroronmissingdir: false) {
exclude(name: "**/*.java")
}
}
}
}
}
// end::xsd[]
task afterEclipseImport {
dependsOn "genJaxb"
}
// tag::jaxb[]
configurations {
jaxb
}
bootJar {
baseName = 'gs-producing-web-service'
version = '0.1.0'
from genJaxb.classesDir
}
// tag::dependencies[]
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-web-services")
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("wsdl4j:wsdl4j:1.6.1")
jaxb("org.glassfish.jaxb:jaxb-xjc:2.2.11")
compile(files(genJaxb.classesDir).builtBy(genJaxb))
}
// end::dependencies[]
// end::jaxb[]
答案 0 :(得分:2)
从此处导入项目时,我遇到了相同的错误:https://spring.io/guides/gs/producing-web-service
在智能上,右键单击该项目,然后向下滚动至maven选项。
单击“生成源和更新文件夹” ,这将为项目生成域类。
答案 1 :(得分:1)
这些io.spring.guides.gs_producing_web_service
包中的类是由genJaxb
任务生成的:生成的源文件位于${buildDir}/generated-sources/jaxb
中,而编译后的类位于${buildDir}/classes/jaxb
中。
为了生成这些源和类,只需执行Gradle任务build
,这将触发genJaxb
的执行
在第一次构建后,您将需要执行“刷新Gradle项目”,以使生成的类在Eclipse中“可见”,然后应修复导入错误。
答案 2 :(得分:0)
我从上下文菜单“ Spring Tools / Update Maven Dependencies”中选择,然后在“ Configure / Add gradle nature”中选择问题。 谢谢!