SpringBoot 学习——SpringBoot 入门 HelloWorld

贫寒豌豆

共 2033字,需浏览 5分钟

 · 2017-04-11

SpringBoot学习——SpringBoot入门HelloWorld

相关资源

官网地址:http://projects.spring.io/spring-boot/

创建maven项目

勾选箭头处,创建一个简单的项目

填写groupId和artifactId,点击确定

配置pom.xml

官方网址有相关配置,本文采用1.5.1版本的,完整pom
注意,如果要打成可执行jar包,要指定jar包的主函数


    4.0.0
    HelloConsumer
    HelloConsumer
    0.0.1-SNAPSHOT

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

    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.apache.maven.plugins
                maven-jar-plugin
                
                    
                        
                            true
                            com.pzr.consumer.test.OfficialDemo
                        
                    
                
            
        
    

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

编写测试java类

package com.pzr.consumer.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 官方例子
 * @author pzr
 *
 */
@Controller
@EnableAutoConfiguration
public class OfficialDemo {
    @RequestMapping("/test")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(OfficialDemo.class, args);
    }
}

更新maven项目

pom.xml修改完成后,需要更新项目
项目右键-Maven4MyEclipse-Update project或者快捷键alt+F5

完整的项目结构

运行java

运行OfficialDemo

在浏览器地址栏输入:http://localhost:8080/test

运行jar

清理-编译-打包

右键项目-Run As-Maven build

在Goals写入clean再点击Run
清理成功后,执行上个步骤,写入compile,点击Run
编译成功后,执行上个步骤,写入package,点击Run

打包成功后,会在项目的target目录下生成对应的jar包

运行

将jar包拷贝到c盘下,打开命令控制台alt+R,输入cmd回车
到c盘目录下运行命令

在浏览器地址栏输入:http://localhost:8080/test

出现问题

打包时一定要在pom中指定运行主函数

浏览 29
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报