Spring Boot 开发入门


文章目录

  • Spring Boot 开发入门
  • 一.任务要求
  • 1.helloworld web
  • 2. RESTful 接口的 Web服务
  • 二.任务过程
  • 1.helloworld web
  • (一)创建项目
  • (二)编写代码
  • (三)测试结果
  • 2.RESTful 接口的 Web服务
  • (一)编写代码
  • (二)用Postman进行测试


一.任务要求

1.helloworld web

在Idea上创建基于Spring Boot的web 项目,当客户端浏览器访问该web资源时,返回的网页显示 “helloword Spring Boot!这是一个用Spring Boot开发的网站。”

2. RESTful 接口的 Web服务

1、初步了解HTTP协议,掌握B/S之间的 请求Request、服务响应Response和get、put、post等主要概念和技术方法。
2、在Idea上创建基于Spring Boot的web 项目,当客户端浏览器分别用get、put、post等访问该web服务资源时,服务器能做出正确的结果。
3、安装Postman 软件,用Postman对你的web进行测试。

二.任务过程

1.helloworld web

(一)创建项目

点击Create New Project选择Spring Initializr并Next

springboot开发教程 spring boot开发教学_maven

设置好Group,Artifact名字,点击next

springboot开发教程 spring boot开发教学_java_02

勾选Web,Spring Web,点击next

springboot开发教程 spring boot开发教学_maven_03

(二)编写代码

POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。事实上,在Maven世界中,project可以什么都没有,甚至没有代码,但是必须包含pom.xml文件。

springboot开发教程 spring boot开发教学_springboot开发教程_04

在主程序包下新建一个controller包并且新建一个HelloController类,并写下代码

package com.example.Controller;

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

@RestController@RequestMapping("/hello")
public class HelloController {

    @GetMapping("/say")
    public String sayHello(){
        return "helloword Spring Boot!这是一个用Spring Boot开发的网站。";
    }
}

springboot开发教程 spring boot开发教学_spring_05

(三)测试结果

程序启动成功

springboot开发教程 spring boot开发教学_spring_06

在浏览器中输入

http://localhost:8080/hello/say

springboot开发教程 spring boot开发教学_spring boot_07

使用IDEA新建基于SpringBoot的Web项目完成!

2.RESTful 接口的 Web服务

(一)编写代码

同上,创建一个springboot项目,然后添加四个类并编写代码

springboot开发教程 spring boot开发教学_java_08


Count:

package com.example.bean;

public class Count {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

ResourceController:

package com.example.controller;

import com.example.bean.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.service.ResourceService;

@RestController
public class ResourceController {

    @Autowired
    ResourceService resourceService;

    @RequestMapping(value = "/me/count", method = RequestMethod.PUT)
    @ResponseBody
    public void initCount(@RequestBody Count count){
        resourceService.initCount(count);
    }

    @RequestMapping(value = "/me/count", method = RequestMethod.POST)
    @ResponseBody
    public void modifyCount(@RequestBody Count count){
        resourceService.addCount(count);
    }

    @RequestMapping(value = "/me/count", method = RequestMethod.GET)
    @ResponseBody
    public  Count getCount()
    {
        return resourceService.getCount();
    }
}

ResourceManager:

package com.example.manager;

public class ResourceManager {
    private int count = 0;

    private static ResourceManager instance = new ResourceManager();

    private ResourceManager(){}

    public static ResourceManager getInstance(){
        return instance;
    }

    public synchronized void addCount(int i){
        count = count + i;
    }

    public synchronized  void minusCount(int i){
        count = count -i;
    }

    public int getCount(){
        return count;
    }

    public void initCount(int i){
        count = i;
    }
}

ResourceService:

package com.example.service;

import com.example.bean.Count;
import com.example.manager.ResourceManager;
import org.springframework.stereotype.Service;

@Service
public class ResourceService {
    public void addCount(Count count){
        if (count != null){
            ResourceManager.getInstance().addCount(count.getCount());
        }
    }

    public void minusCount(Count count){
        if (count != null) {
            ResourceManager.getInstance().minusCount(count.getCount());
        }
    }

    public Count getCount()
    {
        Count count = new Count();
        count.setCount(ResourceManager.getInstance().getCount());
        return count;
    }

    public void initCount(Count count){
        if (count != null) {
            ResourceManager.getInstance().initCount(count.getCount());
        }
    }
}

程序正常启动

springboot开发教程 spring boot开发教学_java_09

springboot开发教程 spring boot开发教学_spring boot_10

(二)用Postman进行测试

首先使用get接口进行查询

springboot开发教程 spring boot开发教学_springboot开发教程_11

用put接口初始化数据

springboot开发教程 spring boot开发教学_spring boot_12

再次使用get接口查询

springboot开发教程 spring boot开发教学_springboot开发教程_13


用post结果修改数据

springboot开发教程 spring boot开发教学_spring boot_14

再次使用get接口查询

springboot开发教程 spring boot开发教学_spring boot_15

用post接口输入负值再次测试

springboot开发教程 spring boot开发教学_springboot开发教程_16


再次使用get接口查询

springboot开发教程 spring boot开发教学_springboot开发教程_17


测试成功!