美文网首页
01.Spring Boot 入门

01.Spring Boot 入门

作者: s酸菜 | 来源:发表于2018-08-27 20:59 被阅读0次
1. Spring Boot介绍

Java后台每一个层级的解决方案有不同的框架,由于每一个框架有自己的配置,所以Spring Boot 的出发点就是整合三个层级这些臃肿的配置。它是Spring 社区较新的一个项目。该项目的目的是帮助开发者更容易的创建基于 Spring 的应用程序和服务,让更多人的人更快的对 Spring 进行入门体验,为 Spring 生态系统提供了一种固定的、约定优于配置风格的框架

2.Spring Boot 环境搭建
  1. Spring Boot 官网

    https://spring.io/

    使用文档:projects -> Spring Boot -> learn -> 1.5.15 GA -> Reference Doc.

    GA 代表稳定版本

  2. build.gradle 添加依赖

    dependencies {
        compile("org.springframework.boot:spring-boot-starter-web:1.5.15.RELEASE")
        compile group: 'com.alibaba', name: 'fastjson', version: '1.2.45'
    }
    
  3. 定义SpringBoot 启动类

    /**
     * @ 创建者:   ty
     * @ 时间:    2018/8/17 14:11
     * @ 描述:
     */
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
    }
    
  4. 编写第一个Controller

    /**
     * @ 创建者:   ty
     * @ 时间:    2018/8/17 14:12
     * @ 描述:
     */
    @Controller
    public class LoginController {
    
        @RequestMapping("/login")
        public String login(String username, String password, HttpServletResponse response) {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
    
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 1);
            jsonObject.put("message", "ok");
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            jsonObject.put("data", user);
            PrintWriter writer = null;
            try {
                writer = response.getWriter();
                writer.print(jsonObject);
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                assert writer != null;
                writer.flush();
                writer.close();
            }
        }
    }
    
3.启动项目

启动成功如下图所示:

spring boot 启动成功.png
4.测试接口

http://localhost:8080/login?username=springboot&password=123456

测试数据.png

相关文章

网友评论

      本文标题:01.Spring Boot 入门

      本文链接:https://www.haomeiwen.com/subject/hxtxwftx.html