为什么org.springframework.boot不能导入,而org.springframework.web可以导入?

爱丽儿

我试图建立这个RESTful服务例如:https://spring.io/guides/gs/rest-service/GreetingController.java与进口没有任何错误:

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {
    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(value = "/greeting", method = RequestMethod.GET)
    public Greeting greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

但是随着Application.java我得到:带有错误的源代码图像

我收到以下错误:The import org.springframework.boot cannot be resolved.我将带有所有jar的Spring 4库添加到了项目中。为什么第一次导入有效而第二次导入无效?@ComponentScan注释也没有错误,但是注释有错误@EnableAutoConfiguration

爱丽儿

我终于设法使它工作了。问题出在pom.xmlSpring网站上发布文件中:它需要进行一些修改。这是我使用的版本:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <properties>
        <start-class>hello.Application</start-class>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

然后,我继续使用Eclipse插件Maven构建项目。有一个进程正在占用8080端口,所以我将其杀死。我运行.jar了从Maven构建中获得的代码,它起作用了!谢谢你们指出问题。我需要Maven来定义依赖关系,而不是手动包含库。我认为我迟早必须学习Maven。:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

org.springframework.boot.web.support不存在

org.springframework.web.client.HttpClientErrorException:400错误的请求

错误[org.springframework.web.servlet.DispatcherServlet]

“导入org.springframework无法解析。”

无法导入org.springframework.data.rest.webmvc.ResourceNotFoundException;

org.springframework.boot.spring-boot POM的用途是什么?

导入org.springframework.security无法解析

org.springframework.web.multipart.MultipartFile和org.springframework.core.io.Resource之间的转换

导入org.springframework.web无法解析。蚀

导入org.springframework.boot.autoconfigure.web.ErrorAttributes无法解析

org.springframework.dao.DataAccessResourceFailureException

为什么org.springframework.http.ResponseEntity不实现Serializable

导入org.springframework.data无法在gradle中解析

org.springframework.data.domain.Sort为什么出错

导入到IntelliJ-错误包org.springframework.boot不存在

无法导入org.springframework.test.context.ActiveProfiles;

org.springframework.jdbc.CannotGetJdbcConnectionException

找不到org.springframework.web.context.ContextLoaderListener类

ClassNotFoundException :: org.springframework.web.context.ContextLoaderListener

org.springframework.web.servlet.DispatcherServlet noHandlerFound

错误org.springframework.web.HttpMediaTypeNotSupportedException

org.springframework.web.multipart.commons.CommonsMultipartFile无法转换为org.springframework.web.multipart.MultipartFile

org.springframework.beans.NotReadablePropertyException:

org.apache.catalina.connector.RequestFacade 不能转换为 org.springframework.web.multipart.MultipartHttpServletRequest

找不到 org.springframework.web.WebApplicationInitializer

无法导入 org.springframework.web.multipart.MultipartFile;

org.springframework.boot.web.support.does 不存在

org.springframework.data.repository 可以被 Hibernate 管理吗?

无法导入 org.springframework.web.bind.annotation.RestController;不能使用@Restcontroller