Spring Boot-(1)先跑起来

2017-10-25  本文已影响100人  会点代码的大叔

最好的参考文档: Spring boot的官方文档

最近在跟进一个项目,用的是Spring Cloud一套的东西,一些东西研究起来比较吃力,还是对一些基础的知识了解的不够深入,比如Spring Boot,所以干脆重新研究一遍。

用比较Low的方式新建这个项目,高大上点儿的话可以使用https://start.spring.io/来创建项目,先还是使用手动创建的方式。

其实直接参考官方的Quick Start即可.

1.新建一个maven工程。

<groupId>com.spring.lw</groupId>
<artifactId>Towns</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

2.引入jar包:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

最好能指定一下JDK的版本

<build>
    <plugins>
        <!-- compiler插件, 设定JDK版本 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <showWarnings>true</showWarnings>
            </configuration>
        </plugin>
    </plugins>
</build>

3.增加一些包路径,目前工程的目录结构为:

image.png

4.增加TownsApplication.java,这个是Spring boot的入口:

package com.spring.lw.Towns;

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

@SpringBootApplication  
//same as @Configuration @EnableAutoConfiguration @ComponentScan
public class TownsApplication {
    public static void main( String[] args ){
        SpringApplication.run(TownsApplication.class, args);
    }
}

5.实体类:

package com.spring.lw.Towns.vo;

import lombok.Data;

@Data
public class User {
    private int id;
    
    private String username;
    
    private String password;
    
    private String managecom ;
    
    public String toString(){
        return "id = " + id 
                + " , username = " + username 
                + ", password = " + password  
                + ", managecom = " + managecom ;
    }
}

6.暴露对外的Rest接口:

package com.spring.lw.Towns.controller;

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

import com.spring.lw.Towns.vo.User;

@RestController
@RequestMapping(value="/user")
public class UserController {
    
    @RequestMapping(value="/get")
    public User getUser(){
        User user = new User();
        user.setUsername("Towns");
        user.setManagecom("US");
        user.setPassword("123456");
        user.setId(10001);
        
        return user ;
    }
}

7.TownsApplication.java-->run as Java Application,运行后浏览器可以访问:

http://10.136.19.170:8080/user/get

浏览器返回:{"id":10001,"username":"Towns","password":"123456","managecom":"US"}

知识点(抄的官方文档,加上自己的理解和翻译):

  1. @RestController and @RequestMapping annotations
  1. @EnableAutoConfiguration annotation
上一篇下一篇

猜你喜欢

热点阅读