码迷,mamicode.com
首页 > Web开发 > 详细

添加对json存储数据的处理及实战演示(V1.0.1)

时间:2018-10-28 11:16:20      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:err   nod   wired   attribute   添加   分享   content   log   message   

技术分享图片技术分享图片

原来的tao-dreamhouse改名为tao-disabuse,没有特别的原因,就觉得比较符合我想做的-3-(注意修改相关节点名称

对tao-common的处理

package cn.zytao.taosir.common.model.disabuse;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@Entity
@ApiModel(value="疑惑信息")
public class Confusion implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 4105144790292919691L;
    
    @Id
    @ApiModelProperty(name ="id",value = "ID主键")
    private String id;
    
    @ApiModelProperty(name ="theme",value = "主题")
    private String theme;
    
    @ApiModelProperty(name ="content",value = "内容")
    private String content;
    
    @ApiModelProperty(name="question",value = "问题")
    private String question;
    
    @ApiModelProperty(name ="status",value = "当前状态:0待解惑-1正解惑-2已解惑")
    private Integer status;
    
    @ApiModelProperty(name ="details",value = "进度详情,json")
    private String details;
    
    @ApiModelProperty(name ="praise",value = "点赞数")
    private Integer praise;
    
    @ApiModelProperty(name ="presenter",value = "提出者")
    private String presenter;
    
    @ApiModelProperty(name ="answer",value = "采纳解答,json")
    private String answer;
    
    @ApiModelProperty(name ="createTime",value = "提出时间")
    private Date createTime;
    
    @ApiModelProperty(name ="endTime",value = "解惑时间")
    private Date endTime;
}

添加该实体类,并在数据库新增相关表

package cn.zytao.taosir.common.service.disabuse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;

import cn.zytao.taosir.common.model.disabuse.Confusion;

public interface ConfusionInterface {
    
    /**
     * 提出疑惑
     * @param confusion
     * @return
     */
    @RequestMapping(value="/confusion/solve",method=RequestMethod.POST)
    JSONObject solve(@RequestBody Confusion confusion);
    
    /**
     * 解答疑惑
     * @param answer
     * @return
     */
    @RequestMapping(value="/user/answer",method=RequestMethod.PUT)
    JSONObject answer(@RequestParam(value="id") String id,@RequestParam(value="key") String key,@RequestBody JSONObject answer);
    
    /**
     * 根据ID获取困惑相关信息
     * @param id
     * @return
     */
    @RequestMapping(value="/user/getConfusionById",method=RequestMethod.GET)
    JSONObject getConfusionById(@RequestParam(value="id") String id);
}

定义相关接口及要演示的方法

添加技术分享图片关于DataHandleUtils的相关说明,见 https://www.cnblogs.com/it-taosir/p/9860829.html

package cn.zytao.taosir.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class DataHandleUtils {    
    /**
     * UUID创建随机主键
     * @param digits    位数
     * @return
     */
    public static String createRandNum(int digits) {
        return UUID.randomUUID().toString().replace("-", "").trim().substring(0,digits);
    }
    
    /**
     * 创建字符串日期
     * @param format    格式
     * @return
     */
    public static String createDate(String format) {
        return new SimpleDateFormat(format).format(new Date());
    }
}

服务提供者tao-disabuse方面

package cn.zytao.taosir.disabuse.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import cn.zytao.taosir.common.model.disabuse.Confusion;

public interface ConfusionRepository extends JpaRepository<Confusion,String>, JpaSpecificationExecutor<Confusion>{

}
package cn.zytao.taosir.disabuse.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.zytao.taosir.common.model.disabuse.Confusion;
import cn.zytao.taosir.common.service.disabuse.ConfusionInterface;
import cn.zytao.taosir.common.util.ActionHelper;
import cn.zytao.taosir.common.util.DataHandleUtils;
import cn.zytao.taosir.common.util.JsonHandleUtils;
import cn.zytao.taosir.disabuse.dao.ConfusionRepository;

@RestController
public class ConfusionServiceImpl implements ConfusionInterface{

    @Autowired
    private ConfusionRepository confusionRepository;
    
    @Override
    public JSONObject solve(@RequestBody Confusion confusion) {
        int randNum = new Random().nextInt(6);
        confusion.setId(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+
                UUID.randomUUID().toString().replace("-", "").trim().substring(randNum, randNum+6));
        confusion.setStatus(0);
        confusion.setDetails(new JSONArray().toJSONString());
        confusion.setCreateTime(new Date());
        confusion.setPraise(0);
        confusionRepository.save(confusion);
        return ActionHelper.responseOk();
    }

    @Override
    public JSONObject answer(String id, String key,@RequestBody JSONObject answer) {
        answer.put("id",DataHandleUtils.createRandNum(5));
        answer.put("time",DataHandleUtils.createDate("yyyy-MM-dd hh:mm:ss"));
        Confusion confusion = confusionRepository.getOne(id);
        JSONArray jsonArray = JSONArray.parseArray(confusion.getDetails());
        if("-1".equals(key)) {
            jsonArray.add(firstAnswer(confusion.getQuestion(), answer));
            confusion.setDetails(jsonArray.toJSONString());
            confusion.setStatus(1);
            confusionRepository.save(confusion);
            return ActionHelper.responseOk();
        }
        JsonHandleUtils.addNode(jsonArray, "id", key, -1, answer);
        confusion.setDetails(jsonArray.toJSONString());
        confusion.setStatus(1);
        confusionRepository.save(confusion);
        return ActionHelper.responseOk();
    }
    
    @Override
    public JSONObject getConfusionById(String id) {
        return ActionHelper.responseOk(confusionRepository.getOne(id));
    }

    private JSONObject firstAnswer(String message,JSONObject answer) {    
        JSONObject node=new JSONObject();
        node.put("id", DataHandleUtils.createRandNum(5));
        node.put("user",answer.getString("user"));
        node.put("message", message);
        node.put("time", DataHandleUtils.createDate("yyyy-MM-dd hh:mm:ss"));
        JSONArray children=new JSONArray();
        children.add(answer);
        node.put("children", children);
        return node;
    }
}

公用API接口 tao-api:

package cn.zytao.taosir.api.service.disabuse;

import org.springframework.cloud.openfeign.FeignClient;

import cn.zytao.taosir.common.service.disabuse.ConfusionInterface;

@FeignClient(name="taosir-disabuse")
public interface ConfusionServiceAPI extends ConfusionInterface{

}
package cn.zytao.taosir.api.controller.disabuse;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.zytao.taosir.api.service.disabuse.ConfusionServiceAPI;
import cn.zytao.taosir.common.model.disabuse.Confusion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@Api(value = "疑惑管理" ,tags = {"疑惑信息管理的接口"})
@RequestMapping("confusion")
public class ConfusionController {

    @Autowired
    private ConfusionServiceAPI confusionService;
    
    @RequestMapping(value="/solve",method=RequestMethod.POST)
    @ApiOperation(value="提出疑问", notes="提供相关疑惑信息提出疑惑")
    public JSONObject solve(@ModelAttribute Confusion confusion) {
        return confusionService.solve(confusion);
    }
    
    @RequestMapping(value="answer/{id}/{key}",method=RequestMethod.PATCH)
    @ApiOperation(value="解答疑惑", notes="提供相关信息解答疑惑")
    public JSONObject answer(
            @ApiParam(required = true, value = "疑惑ID") @PathVariable String id,
            @ApiParam(required = true, value = "详情对应节点ID")@PathVariable String key,
            @ApiParam(required = true, value = "解答信息")@RequestBody Map<String, Object> map) {
        return confusionService.answer(id, key, new JSONObject(map));
    }
    
    @RequestMapping(value="getInfo/{id}",method=RequestMethod.GET)
    @ApiOperation(value="获取疑惑信息", notes="根据ID获取困惑的相关信息")
    public JSONObject getInfo(@PathVariable String id) {
        return confusionService.getConfusionById(id);
    }
}

演示:

根据顺序启动服务:访问swagger2 http://localhost:8765/swagger-ui.html#/

技术分享图片

提出疑问:

技术分享图片

技术分享图片

技术分享图片

技术分享图片

从数据库拿到ID调用获取疑惑信息方法查看:

技术分享图片

解答疑惑:

技术分享图片

再次查看该困惑的详情:

技术分享图片

我们的解答已经以json树格式存进相关详情字段

至此完成任务

 

添加对json存储数据的处理及实战演示(V1.0.1)

标签:err   nod   wired   attribute   添加   分享   content   log   message   

原文地址:https://www.cnblogs.com/it-taosir/p/9864126.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!