springboot入门程序详解

springboot程序 

工具

 maven

 sts

 java8(微服务)

1.首先springboot它是一个快速搭建spring的框架(使用java8的微服务技术),相比较springmvc而言少了更多的配置,因为它已经封装了springmvc.因此使用maven开发我们只需要引入的依赖即可

<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.bldz.springboot</groupId>
  <artifactId>springboot-helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>springboot-helloworld</name>
  <url>http://maven.apache.org</url>


  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

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

    <dependencies>
        <!-- Spring Boot web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>
        	<groupId>junit</groupId>
        	<artifactId>junit</artifactId>
        </dependency>
    </dependencies>
</project>

2.由于springboot底层使用的是springmvc,因此我们在写controller层的时候,需要引入注解@RestController,这里的@RestController 相当于 springmvc的 @controller和@RequestBody,代码如下

package org.spring.springboot;

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

/**
 * 
 * @RestController 相当于 springmvc的 @controller和@RequestBody
 * @author qinxw
 *
 */
@RestController
public class HelloWorldController {
	@RequestMapping("/")
	public String hello() {
		return "hello";
	}
}

3.就是写一个springboot的启动类,并使用springboot框架的@SpringBootApplication注解标记为这是springboot的启动类,详细代码如下

package org.spring.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * springboot 启动类
 * @author qinxianwei
 *
 */
@SpringBootApplication
public class App{
    public static void main(String[] args ){
        SpringApplication.run(App.class, args);
    }
}

4.运行main方法,测试结果如下

 

 

 

码云代码下载链接 

     https://git.oschina.net/ymdrqq/springboot-helloworld.git

猜你喜欢

转载自chuichuige.iteye.com/blog/2378651