在Spring Boot 2.2.0应用程序中遇到REST端点问题

时间:2019-10-19 17:02:28

标签: java rest spring-boot

我尝试使用GraphQL和MongoeDB创建一个Spring Boot REST应用程序(我从该文章https://medium.com/oril/spring-boot-graphql-mongodb-8733002b728a开始)。应用程序启动,但是当我尝试将某些内容发布到端点时出现404错误。

我还阅读到2.2.0不再支持@PostConstruct(这是另一个问题,我稍后会尝试解决,我尝试在启动期间预加载数据库)。

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.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tsoai-api</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <lombok.version>1.18.10</lombok.version>
        <commons-io.version>2.5</commons-io.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </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>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io.version}</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-spring-boot-starter</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphql-java-tools</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.graphql-java</groupId>
            <artifactId>graphiql-spring-boot-starter</artifactId>
            <version>3.6.0</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我的主应用程序文件如下所示(没什么特别的!):

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

MainController.java

@RestController
@RequestMapping(path = "/query")
public class MainController {

    private GraphQL graphQL;
    private GraphQlUtility graphQlUtility;

    @Autowired
    MainController(GraphQlUtility graphQlUtility) throws IOException {
        this.graphQlUtility = graphQlUtility;
        graphQL = graphQlUtility.createGraphQlObject();
    }

    @PostMapping(path= "/")
    public ResponseEntity query(@RequestBody String query){
        ExecutionResult result = graphQL.execute(query);
        System.out.println("errors: "+result.getErrors());
        return ResponseEntity.ok(result.getData());
    }

}

当我在http://localhost:8080/query上发帖时,出现此错误:

{
    "timestamp": "2019-10-19T16:10:19.136+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/query"
}

3 个答案:

答案 0 :(得分:1)

此错误的原因可能是Spring Boot未扫描此类。请尝试在Spring Boot主类上添加ComponentScan批注,并在其中包含用于保存控制器的软件包,如下所示。 @ComponentScan(basePackages = {“ com.example”})

答案 1 :(得分:1)

它将仅匹配/query/而不匹配/query。如果要同时匹配/query/query/,则可以将path中的@PostMapping保留为默认查询方法。

@RestController
@RequestMapping(path = "/query")
public class MainController {

    @PostMapping
    public ResponseEntity query(@RequestBody String query){

    }

}

答案 2 :(得分:0)

检查运行Spring Server的端口,根据媒体报道,他们已将其配置为在:8080 上运行在:8000 。 该错误表明它找不到映射到此api端点的任何控制器。

http://localhost:8000/query/

`