研磨SpringCloud系列(一)第一个Spring Boot应用

在此之前,给大家推荐几个东西.

STS,Spring官方基于eclipse做的扩展ide.Spring官方背书.

第二个,lombok,注解生成get/set,构造以及基本方法的插件,"隐藏大量非业务代码",可以让我们的业务代码变的更纯净,更加容易维护.

第三个,推荐一下JPA,现在也越来越流行.

总之呢,目的就是让我们的代码阅读性更强,规范我们的开发.

 

下面编写我们第一个Spring boot应用

先构建一个maven项目,在pom.xml文件中添加springboot web依赖

<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</groupId>
    <artifactId>SrpingBoot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot</name>
    <description>第一个SpringBoot应用</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

 

接下来我们新建一个springboot启动类,用@SpringBootApplication标识它

package com;

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

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

}

 

新建一个Controller类,提供网页访问接口,@RestController注解默认以json格式返回.

package com.controller;

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

@RestController
public class Controller {
    
    @RequestMapping(name="/add" ,method=RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        return a+b;
    }
}

 

运行Application类的main方法,我们第一个springboot 应用就创建成功了,是不是超级方便呢

接下来我们访问以下localhost:8080/add?a=1&b=2看以下吧~

发布了0 篇原创文章 · 获赞 7 · 访问量 8万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章