简体   繁体   中英

Spring Batch: Cannot recognize ItemProcessor Interface

I am a beginner just started learning Spring Batch. I followed this tutorial here to create a helloworld example. While I was following the tutorial, I encountered a problem when I was trying to import org.springframework.batch.item.ItemProcessor into a java class. Hence I searched online and found out that I need to add something inside the build.gradle. The problem was, even though I have added the org.springframework.batch:spring-batch-core dependency in build.gradle, I was still having error message for the import. I am using Eclipse Java EE IDE 4.5.0 (Mars) to do this tutorial and have installed the Spring Test Suite (by following the tutorial). I have also tried right clicking the project and choose the "refresh" option but the error message is still there.

Additional note: I have also tried to follow the answer stated here but I cannot find the "Refresh Dependencies" in the "Gradle" context menu. How do you even know that the build.gradle is working?

Below is my code (Java class) that imports the Spring Batch:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//error message: "the import org.springframework.batch cannot be resolved"
import org.springframework.batch.item.ItemProcessor; 


//error message: "ItemProcessor cannot be resolved to a type"
public class PersonItemProcessor implements ItemProcessor<Person, Person> {

    @Override
    public Person process(final Person person) throws Exception {
        //some codes
    }
}

Below is the build.gradle file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-consuming-rest'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.batch:spring-batch-core") //added by me
    compile("org.springframework.boot:spring-boot-starter")
    compile("org.springframework:spring-web")
    compile("com.fasterxml.jackson.core:jackson-databind")
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

I had the same problem with the same tutorial except that I use IntelliJ instead of Eclipse. On IntelliJ there is a "Gradle" panel on the right side that you can open and click refresh. This will refresh your gradle. It solved the problem in my case. (Rebuilding gradle.build didn't)

指向gradle面板的红色箭头

指向刷新图标的红色箭头

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM