spring 入门demo

 

相关资源

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

创建maven项目

勾选箭头处,创建一个简单的项目 
 
填写groupId和artifactId,点击确定 

配置pom.xml

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

<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>HelloConsumer</groupId> <artifactId>HelloConsumer</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 引用依赖的父包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <!--maven的插件 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 打成jar包后所执行的主函数 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.pzr.consumer.test.OfficialDemo</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> <!-- 依赖包 --> <dependencies> <!-- spring boot需要的包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

 

编写测试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中指定运行主函数

posted on 2018-06-03 16:23  shoshana~ 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/shoshana-kong/p/9129514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值