简体   繁体   中英

Katharsis in combination with Spring Boot

I have implemented a REST API using Spring Boot (version 1.3.6.RELEASE), which is working as expected. However I would like to add JSON API support to my application.

The problem is that I get a 404 when trying to execute a GET on a Katharsis resource.

The dependencies in pom.xml look as follows (I use Spring Security too):

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-jdbc</artifactId>
    </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.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.4.1</version>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${hsqldb.version}</version>
    </dependency>
    <!--SpringFox dependencies -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity-tools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dumbster</groupId>
        <artifactId>dumbster</artifactId>
        <version>1.6</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.katharsis</groupId>
        <artifactId>katharsis-spring</artifactId>
        <version>2.6.0</version>
    </dependency>
    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.9</version>
    </dependency>
</dependencies>

My application initialisation class is as follows:

@SpringBootApplication
@Configuration
@EnableAutoConfiguration(exclude = WebMvcAutoConfiguration.class)
@EnableSwagger2
@ComponentScan(basePackages = "com.myapp")
@Import({KatharsisConfigV2.class})
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

The resource class:

@ApiModel(description = "")
@JsonApiResource(type = "foo-data")
public class FooData extends DomainRepresentation {
    @JsonApiId
    private String id = null;
    @JsonProperty
    private String name = null;
    // .... getters & setters
}

ResourceRepository class:

@JsonApiResourceRepository(FooData.class)
@Component
public class FooDataKatApi extends MyApi {
    @Autowired
    private FooDataService fooDataService;

    @JsonApiFindOne
    public FooData foodataIdGet(String id, QueryParams requestParams) { 
        FooData result = fooDataService.getFooDataById(id);
        return result;
    }
}

If I add Spring @ResourceMapping s than I get data back but without Katharsis additions like "link", etc. It's as if the API call is not being picked up by Katharsis, but it's handled by Spring.

Any help would be appreciated.

-- EDITTED --

The FooDataService is as follows:

@Service
public class FooDataService extends MyService {
    @Autowired
    private FooDataRepository fooDataRepository;

    public FooData getFooDataById(String id) {
        FooDataEnt fooDataEnt = fooDataRepository.findByFooDataId(id);

        if (fooDataEnt != null) {
            return convertFooDataEntToFooData.apply(fooDataEnt);
        }

        throw new NotFoundException("Foo not found");
    }
}

The FooDataRepository extends Spring's PagingAndSortingRepository.

Katharsis config in application.yaml:

katharsis:
    resourcePackage: com.my.package
    domainName: http://localhost:8080
    pathPrefix: /api/kat

The request and response from PostMan

-- UPDATE 1 --

Apparently the configured value for katharsis.pathPrefix was not correct. Web application's context-root is "/api" and the value of katharsis.pathPrefix should be only "/kat".

My next problem is that a RepositoryNotFoundException is being throw while handling the request, while the Resource class is found already.

I solved the riddle by debugging the code and finding out that my Katharsis config was not correct. Katharsis only picked up the resource classes but not the resource repository classes. When I changed the katharsis.resourcePackage to a higher level where the resource repositories resided, the problem got solved and Katharsis picked up all related annotated classes.

I don't see your foo data service implementation anywhere. However, I am 99% sure you're not passing the json api accept type.

Accept: application/vnd.api+json

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