软件设计与体系结构大作业-基于SpringBoot的REST API设计

任务分配

|姓名|贡献分|贡献|
|-|-|-|
|魏啸冲|20|后端逻辑处理、minecraft服务器对接|
|张夏楠|20|api设计,前后端api对接|
|李芊颖|20|游戏逻辑设计、测试工作|

Minecraft披萨点单展示

整体架构示意图:

说明:

即用户在游戏中(Minecraft Client,下简称MC)进行点单,游戏客户端通过TCP协议与游戏服务器保持连接(Minecraft Server,下简称MS),当用户点击提交订单按钮时,MC会往MS发送一个事件,这个事件由我编写的MS Spigot插件监听,并向披萨服务器(Pizza Server,下简称PS)提供的REST API发送一个HTTP请求,然后PS开始处理订单,处理完成后以json格式返回数据给MS,MS将信息整合到游戏物品小票中并发送给玩家,最后玩家在MC中看到小票中的信息。这样就模拟了整个点单过程。
其中点单部分和MS内部工作机制在前两篇报告中已经说明。
Controller层的代码:

package com.example.pizzasystem.control;

import com.example.pizzasystem.service.order.BaseOrder;
import com.example.pizzasystem.service.order.OrderHandler;
import com.example.pizzasystem.service.order.order.AmericaStylePizzaStoreOrder;
import com.example.pizzasystem.service.order.order.BeverageOrder;
import com.example.pizzasystem.service.order.order.ItalyStylePizzaStoreOrder;
import com.example.pizzasystem.service.order.order.Order;
import lombok.Data;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author https://github.com/anlowee
 * @version 1.0
 */
@RestController
public class PizzaOrderController {

    @PostMapping("/api/america/order")
    public String orderPizzasAtAmericaStyleStore(
            @RequestBody RequestBean requestBean) {
        Order pizzaOrdersIssue = new AmericaStylePizzaStoreOrder(requestBean.getPizzas());
        Order beverageOrdersIssue = new BeverageOrder(requestBean.getBeverages());
        OrderHandler orderHandler = new OrderHandler();
        orderHandler.setPizzaOrder(pizzaOrdersIssue);
        orderHandler.setBeverageOrder(beverageOrdersIssue);
        return orderHandler.handleOrder();
    }

    @PostMapping("/api/italy/order")
    public String orderPizzasAtItalyStyleStore(
           @RequestBody RequestBean requestBean
    ) {
        Order pizzaOrdersIssue = new ItalyStylePizzaStoreOrder(requestBean.getPizzas());
        Order beverageOrdersIssue = new BeverageOrder(requestBean.getBeverages());
        OrderHandler orderHandler = new OrderHandler();
        orderHandler.setPizzaOrder(pizzaOrdersIssue);
        orderHandler.setBeverageOrder(beverageOrdersIssue);
        return orderHandler.handleOrder();
    }

}

@Data
class RequestBean {
    private List<BaseOrder> pizzas;
    private List<BaseOrder> beverages;
}

请求体示例:

{
	"pizzas": [
		{
			"type": "Pepperoni",
			"additions": [
				"Large Size",
				"More Cheese",
				"More Cheese",
				"More Pepperoni",
				"High Temperature",
				"Long Time",
				"10 Slices",
				"Dine"
			]
		},
		{
			"type": "Cheese",
			"additions": [
				"Large Size",
				"High Temperature",
				"Long Time",
				"12 Slices",
				"Take Out"
			]
		}
	],
	"beverages": [
		{
			"type": "Milk Tea",
			"additions": [
				"Milk",
				"Mocha",
				"Soy",
				"Whip",
				"Tall Size"
			]
		},
		{
			"type": "Coffee",
			"additions": [
				"Milk",
				"Venti"
			]
		}
	]
}

回复体示例:

##############RECEIPT##############
##################################
pepperoni pizza
use thick crust dough
use mozzarella cheese
use plum tomato sauce
preparing a pepperoni pizza, large size, more cheese, more cheese, more pepperoni
baking a pepperoni pizza, bake at 500C, bake for 45 minutes
cutting a pepperoni pizza, cut to 10 slices
boxing a pepperoni pizza, dine without box
cost: 14.95
##################################
cheese pizza
use thick crust dough
use mozzarella cheese
use plum tomato sauce
preparing a cheese pizza, large size
baking a cheese pizza, bake at 500C, bake for 45 minutes
cutting a cheese pizza, cut to 12 slices
boxing a cheese pizza, take out with box
cost: 8.17
##################################
milk tea
preparing a milk tea, add milk, add mocha, add soy, add whip, tall size
cost: 6.44
##################################
coffee
preparing a coffee, add milk
cost: 1.98
##################################
total cost: 31.54

视频展示:

https://www.bilibili.com/video/BV1Ry4y1z7Vf/
一键三连>_<!

白板企业评估平台

这是我们小组参与开发的另一个比赛项目,其中REST API使用的更多

整体架构:

前后端分离,后端采用三层架构并使用REST API,前端采用Vue多页面架构,前后端用json通信

API 文档:

https://www.evernote.com/shard/s449/client/snv?noteGuid=e60d82ba-695e-485e-bd11-b95708d2e6de&noteKey=e6fb314f5d94f773719b92a74785fdba&sn=https%3A%2F%2Fwww.evernote.com%2Fshard%2Fs449%2Fsh%2Fe60d82ba-695e-485e-bd11-b95708d2e6de%2Fe6fb314f5d94f773719b92a74785fdba&title=Blanker%2BAPI%2B%25E6%2596%2587%25E6%25A1%25A3

Controller层代码展示

UserController:

package com.iamwxc.blanker.module.usermanage;

import com.iamwxc.blanker.common.response.ResponseCode;
import com.iamwxc.blanker.common.response.UnitResp;
import com.iamwxc.blanker.module.usermanage.domain.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author https://github.com/anlowee
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RecaptchaService captchaService;

//    @PostMapping("/api/register")
//    public UnitResp register(@Validated @RequestBody UserVO newUser) {
//        userService.register(newUser);
//        return new UnitResp(ResponseCode.SUCCESS.getCode(), "register success", null);
//    }

    @PostMapping("/api/register")
    public ResponseEntity<?> signup(@Validated @RequestBody UserVO newUser,
                                    @RequestParam(name="g-recaptcha-response") String recaptchaResponse,
                                    HttpServletRequest request){

        String ip = request.getRemoteAddr();
        String captchaVerifyMessage =
                captchaService.verifyRecaptcha(ip, recaptchaResponse);

        if (StringUtils.isNotEmpty(captchaVerifyMessage)) {
            Map<String, Object> response = new HashMap<>();
            response.put("message", captchaVerifyMessage);
            return ResponseEntity.badRequest()
                    .body(response);
        }

        userService.register(newUser);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/api/renew")
    public UnitResp renew(@RequestParam(required = true) Integer level) {
        userService.renewUseTime(level);
        return new UnitResp(ResponseCode.SUCCESS.getCode(), "renew successfully", null);
    }

    @GetMapping("/api/me")
    public UnitResp getCurUserInfo() {
        return new UnitResp(ResponseCode.SUCCESS.getCode(),
                "current user info",
                userService.getUserInfo(SecurityContextHolder.getContext().getAuthentication().getName()));
    }

}

PredictController:

package com.iamwxc.blanker.module.prediction;

import com.iamwxc.blanker.common.response.ResponseCode;
import com.iamwxc.blanker.common.response.UnitResp;
import com.iamwxc.blanker.module.prediction.domain.entity.PredictionRecordEntity;
import com.iamwxc.blanker.module.prediction.domain.vo.BlankerInfoVO;
import com.iamwxc.blanker.module.prediction.domain.vo.PredictionRecordVO;
import com.iamwxc.blanker.module.prediction.domain.vo.PredictionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author https://github.com/anlowee
 */
@RestController
public class PredictController {

    @Autowired
    private PredictService predictService;

    @PostMapping("/api/prediction")
    public PredictionVO doPredict(@Validated @RequestBody BlankerInfoVO blankerInfoVO) throws IOException, InterruptedException {
        return predictService.predict(blankerInfoVO);
    }

    @GetMapping("/api/predictions")
    public UnitResp getPredictRecords() {
        return new UnitResp(ResponseCode.SUCCESS.getCode(), "query ok", predictService.findAllOfCurUser());
    }

    @Secured("ROLE_ADMIN")
    @GetMapping("/api/predictions/all")
    public UnitResp getAllPredictionRecord() {
        return new UnitResp(ResponseCode.SUCCESS.getCode(), "query ok", predictService.findAll());
    }

    @PutMapping("/api/prediction")
    public UnitResp updatePredictRecords(@Validated @RequestBody PredictionRecordVO recordVO) {
        predictService.updatePredictionRecord(recordVO);
        return new UnitResp(ResponseCode.SUCCESS.getCode(), "update ok", null);
    }

}

部分效果展示

posted @ 2020-11-06 10:57  冲冲冲冲冲  阅读(639)  评论(0)    收藏  举报