0

In my GCP Endpoints project @RepositoryRestResource gets injected into the @RestController as null. As a result I get a NullPointer of course. How to inject the properly initiated repository into controller?

Repository class, controller, application and pom follow.

The repository:

package com.katamlek.gcp_crystalloids;

@RepositoryRestResource
public interface PostRepository extends DatastoreRepository<Post, Long> {
}

The controller.

package com.katamlek.gcp_crystalloids;

@Api
@RestController
public class PostController{

    @Autowired
    private PostRepository postRepository;

    // dummy
    @ApiMethod(path = "/hello", httpMethod = ApiMethod.HttpMethod.GET)
    public DummyClass hello() {
        return new DummyClass();
    }

    @ApiMethod(path = "/add", httpMethod = ApiMethod.HttpMethod.POST)
    public Post post(@RequestBody Post post) {
        return postRepository.save(post);
    }

...

And the 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.katamlek</groupId>
    <artifactId>gcp_crystalloids</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>gcp_crystalloids</name>
    <description>Demo project for Crystalloids</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-storage</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-data-datastore</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-data-datastore</artifactId>
        </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-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.google.endpoints</groupId>
            <artifactId>endpoints-framework-all</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.endpoints</groupId>
            <artifactId>endpoints-management-control-appengine-all</artifactId>
            <version>1.0.13</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>javax.servlet</groupId>-->
<!--            <artifactId>javax.servlet-api</artifactId>-->
<!--            <version>3.1.0</version>-->
<!--            <type>jar</type>-->
<!--            <scope>provided</scope>-->
<!--        </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>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>endpoints-framework-maven-plugin</artifactId>
                <version>1.0.2</version>
                <configuration>
                    <!-- plugin configuration -->
                    <hostname>gcpcrystalloids.appspot.com</hostname>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

The application:

package com.katamlek.gcp_crystalloids;

@SpringBootApplication(scanBasePackages = {"com.katamlek"})
@EntityScan(basePackages = {"com.katamlek"})
@EnableDatastoreRepositories(basePackages = {"com.katamlek"})
public class GcpCrystalloidsApplication {

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

}
  • Can you also post your `@SpringBootApplication` class? and the pom.xml or build.gradle ? – gtiwari333 Sep 30 '20 at 17:26
  • 2
    Hello, to be able to understand how Spring Boot will inject stuff we need to have the code of the class which contains main method too. Also, please use package declarations of the classes (because the package structure also has a big impact) – Natan Cox Sep 30 '20 at 17:26
  • https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-locating-the-main-class Please refer to this. Spring boot scans for the classes with reference to the @SpringBootApplication class's package and expects everything under it( unless you want to provide a list of classes/packages to autoscan and create beans) – gtiwari333 Sep 30 '20 at 17:46
  • I edited the question and added pom and packages info. Looking forward to hearing from you! – user14369007 Oct 01 '20 at 05:29

0 Answers0