1.springboot:hello world

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:


    
    org.springframework.boot  
    spring-boot-starter-parent  
    1.3.1.RELEASE  
    

依赖2:设置jdk版本

 
    1.8

依赖3:

  
    
          org.springframework.boot
          spring-boot-starter-web
          
      
  

总文件:


  4.0.0

  com.zhaolixiang
  spring-boot1
  0.0.1-SNAPSHOT
  jar

  spring-boot1
  http://maven.apache.org

  
    UTF-8
    
    1.8
  
  
  
    
    org.springframework.boot  
    spring-boot-starter-parent  
    1.3.1.RELEASE  
    

  
    
      junit
      junit
      3.8.1
      test
    


     
    
          org.springframework.boot
          spring-boot-starter-web
          
      
  
  
   


    
           
               org.springframework.boot
               spring-boot-maven-plugin 
          
      
  

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

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

解决方法:

    
        org.springframework.boot  
        spring-boot-starter-parent  
        1.4.1.RELEASE  
        

改为:

   
        org.springframework.boot  
        spring-boot-starter-parent  
        1.3.1.RELEASE  
        

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

你可能感兴趣的:(1.springboot:hello world)