美文网首页
1.springboot:hello world

1.springboot:hello world

作者: AiPuff | 来源:发表于2017-02-10 14:24 被阅读230次

1.新建一个Maven Java工程

创建后若出现形如下列的问题:

Failure to transfer org.apache.maven:maven-archiver:pom:2.4.2 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are 
 forced. Original error: Could not transfer artifact org.apache.maven:maven-archiver:pom:2.4.2 from/to central (http://repo.maven.apache.org/maven2): connection timed out to http://repo.maven.apache.org/maven2/org/apache/maven/maven-
 archiver/2.4.2/maven-archiver-2.4.2.pom

解决方法:

在M2文件夹里发现maven-archiver没有下载,只有lastupdate文件,删除之;重新project -> Maven - Update Dependencies(快捷键ALT+F5) 问题解决。

如果还有其他错误,解决方法相同。

2.在pom.xml文件中添加Spring BootMaven依赖

依赖1:

<!-- spring boot父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加 -->
  <parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>1.3.1.RELEASE</version>  
  </parent>  

依赖2:设置jdk版本

 <!-- 指定jdk版本,此处为1.8,默认为1.6 -->
    <java.version>1.8</java.version>

依赖3:

  <!--spring-boot-starter-web:为我们打包了:MVC,AOP(面向切面编程)。。的依赖包  -->
    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <!-- 
          <version></version>
          由于我们上面指定了parent(springboot),springboot 会代替我们指定最合适的版本号,所有这里我们就不指定了
           -->
      </dependency>
  </dependencies>

总文件:

<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.zhaolixiang</groupId>
  <artifactId>spring-boot1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-boot1</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <!-- 指定jdk版本,此处为1.8,默认为1.6 -->
    <java.version>1.8</java.version>
  </properties>
  
  <!-- spring boot父节点依赖,引入这个之后相关的引入就不需要添加version配置,spring boot会自动选择最合适的版本进行添加 -->
  <parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>1.3.1.RELEASE</version>  
  </parent>  

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>


     <!--spring-boot-starter-web:为我们打包了:MVC,AOP(面向切面编程)。。的依赖包  -->
    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <!-- 
          <version></version>
          由于我们上面指定了parent(springboot),springboot 会代替我们指定最合适的版本号,所有这里我们就不指定了
           -->
      </dependency>
  </dependencies>
  
  <!--如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。 
  如果使用maven的spring-boot:run的话是不需要此配置的。
  (我在测试的时候,如果不配置下面的plugin也是直接在Main中运行的。) --> 

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

3.新建一个类文件:HelloController.java

package com.zhaolixiang.spring_boot1;

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

/**
 * 在这里我们使用RestController(等价于@Controller和@RequestBody)
 * @author hasee
 *
 */
@RestController
public class HelloController {
    
    /**
     * 这里我们使用@RequestMapping建立请求地址:
     * http://127.0.0.1:8080/hello
     * @return
     */
    @RequestMapping("/hello")
    public String hello(){
        System.out.println("hello进程");
        return "hello";
    }

}

4.设置启动入口:

在App.class类中设置:

public static void main( String[] args )
    {
        /**
         * 在Main方法中启动我们的应用程序
         */
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
       
    }

如果报错:

The type org.springframework.context.ConfigurableApplicationContext cannot be resolved.
It is indirectly referenced from required .class files

原因:你正要使用的类调用了另一个类,而这个类又调用了其他类,这种关系可能会有好多层。而在这个调用的过程中,某个类所在的包的缺失就会造成以上那个错误。

解决方法:

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

改为:

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

5.启动:右键Run As -> Java Application

相关文章

网友评论

      本文标题:1.springboot:hello world

      本文链接:https://www.haomeiwen.com/subject/ifjgittx.html