2017-02-18 51 views
0

我在Spring中開始了一個教程。Spring工具套件中未解決的Spring依賴關係

我下載的春天工具套件,然後創建爲模板提供的demo項目,然後我創建了一個類,並寫了下面的代碼:

package com.example; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.ResponseBody; 

@Controller 
public class WhateverIWant { 

    @RequestMapping("/hi") 
    public @ResponseBody String hiThere() { 
     return "hello world!"; 
    } 
} 

的問題是,ResponseBodyRequestMapping無法識別。

我右鍵單擊該項目,然後執行Run As,然後mvn cleanmvn install,但依然無法識別依賴項。

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.example</groupId> 
    <artifactId>demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.5.1.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

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


</project> 

我怎樣才能使春天工具套件注意的依賴關係?

+0

可以共享的pom.xml? –

+0

我編輯了我的問題。 – octavian

回答

1

問題是ResponseBody和RequestMapping不是 已識別。

您在pom中缺少以下依賴項。

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 
1

增加了Spring的網絡罐子

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.2.3.RELEASE</version> 
</dependency>