Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何将非空@RepositoryRestResource注入@RestController_Spring Boot_Rest_Google Cloud Endpoints - Fatal编程技术网

Spring boot 如何将非空@RepositoryRestResource注入@RestController

Spring boot 如何将非空@RepositoryRestResource注入@RestController,spring-boot,rest,google-cloud-endpoints,Spring Boot,Rest,Google Cloud Endpoints,在我的GCP端点项目中,@RepositoryRestResource作为null注入@RestController。因此,我当然会得到一个空指针。如何将正确启动的存储库注入控制器 存储库类、控制器、应用程序和pom 存储库: package com.katamlek.gcp_crystalloids; @RepositoryRestResource public interface PostRepository extends DatastoreRepository<Post, Lon

在我的GCP端点项目中,@RepositoryRestResource作为null注入@RestController。因此,我当然会得到一个空指针。如何将正确启动的存储库注入控制器

存储库类、控制器、应用程序和pom

存储库:

package com.katamlek.gcp_crystalloids;

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

以及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>

您还可以发布您的
@springbootplication
类吗?还有pom.xml或build.gradle?您好,为了能够理解Spring Boot将如何注入内容,我们需要有包含main方法的类的代码。另外,请使用类的包声明(因为包结构也有很大的影响),请参考此。Spring boot将参照@SpringBootApplication类的包扫描类,并期望它下的所有内容(除非您想向autoscan和create beans提供类/包列表)。我编辑了问题并添加了pom和包信息。期待您的来信!
<?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>
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);
    }

}