有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Maven spring未解析符号

尝试在spring framework上运行一个简单的应用程序时,我在导入web包时遇到了一个问题:

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

我的代码非常基本,我只是想发回一个简单的txt

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        AppServerHandler handler = AppServerHandler.getInstance(6666);


    }

    @RequestMapping(value = "/",method = POST)   <--- not familiar with annonation
    public String index() {
        return AppServerHandler.getWelcomeMsg();
    }

}

波姆。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>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


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

</project>

我真的很想知道会出什么问题。 谢谢


共 (1) 个答案

  1. # 1 楼答案

    请在你的课堂上加入@RestController,如下所示

    @RestController
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
            AppServerHandler handler = AppServerHandler.getInstance(6666);
    
    
        }
    
        @GetMapping(value = "/")   < - use this annotation instead of @Requestmapping
        public String index() {
            return AppServerHandler.getWelcomeMsg();
        }
    
    }
    

    @GetMapping具有HTTP方法GET