您的位置:首页 > 编程语言 > Java开发

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

2017-09-05 15:09 381 查看
在此之前,给大家推荐几个东西.

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看以下吧~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: