SpringBoot学习整理-helloworld

本文详细介绍了如何使用Eclipse和Maven快速搭建Spring Boot项目,包括配置pom.xml文件、创建启动类及控制器,并提供了调试运行及打包部署的方法。
1.Ecliplse创建Maven工程

New Maven Project - Create Simple Project

Group Id: com.yyaat

Artifact Id: sample

Packaging: jar

2.创建pom.xml
<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.yyaat</groupId>
	<artifactId>sample-helloworld</artifactId>
	<version>1</version>
	<packaging>jar</packaging>

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

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

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

		<!-- tomcat 的支持. -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

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

	<!-- 仓库 -->
	<repositories>
		<repository>
			<id>aliyunRepository</id>
			<name>myRepository</name>
			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

项目上右键 maven -update project

创建入口Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		System.out.println("ヾ(◍°∇°◍)ノ゙    项目启动成功      ヾ(◍°∇°◍)ノ゙");
	}
}

创建一个HelloController 两个Get请求 一个返回json 一个返回字符串

import java.util.HashMap;
import java.util.Map;

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

@RestController
public class HelloController {

	@GetMapping("/hello")
	public Map<String,Object> hello(){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("username", "张三");
		resultMap.put("msg", "helloworld");
		return resultMap;
	}
	
	@GetMapping("/greet")
	public String greet(){
		return "helloworld";
	}
}

一.Eclipse中调试运行方式: 运行Application.java 中的main方法

二.打包成jar或war后运行

java -jar xxx.jar
java -jar xxx.war
1.eclipse中使用 maven打包

run as -> maven build...

Goals : package
2.在项目目录执行 maven命令
mvn package

http://localhost:8080/hello

返回结果:{"username":"张三","msg":"helloworld"}

http://localhost:8080/greet

返回结果:helloworld

码云源码地址 https://gitee.com/fedo/spring-boot-study/tree/master/sample-helloword

转载于:https://my.oschina.net/isharecn/blog/1813211

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值