Spring Boot 开发入门

Spring Boot 开发入门

一.任务要求

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
在这里插入图片描述设置好Group,Artifact名字,点击next
在这里插入图片描述勾选Web,Spring Web,点击next
在这里插入图片描述

(二)编写代码

POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,使用XML表示,名称叫做pom.xml。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。事实上,在Maven世界中,project可以什么都没有,甚至没有代码,但是必须包含pom.xml文件。
在这里插入图片描述在主程序包下新建一个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开发的网站。";
    }
}

在这里插入图片描述

(三)测试结果

程序启动成功
在这里插入图片描述在浏览器中输入
http://localhost:8080/hello/say
在这里插入图片描述使用IDEA新建基于SpringBoot的Web项目完成!

2.RESTful 接口的 Web服务

(一)编写代码

同上,创建一个springboot项目,然后添加四个类并编写代码在这里插入图片描述
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());
        }
    }
}

程序正常启动
在这里插入图片描述在这里插入图片描述

(二)用Postman进行测试

首先使用get接口进行查询
在这里插入图片描述用put接口初始化数据
在这里插入图片描述再次使用get接口查询
在这里插入图片描述
用post结果修改数据

在这里插入图片描述再次使用get接口查询
在这里插入图片描述用post接口输入负值再次测试
在这里插入图片描述
再次使用get接口查询在这里插入图片描述
测试成功!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值