java返回响应包装

mode实体定义

package cn.xxx.xxx.mode;

import java.io.Serializable;

/**
 * 功能描述:响应结果类
 *
 *
 */
public class JsonData implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    // 状态码 0 表示成功,1表示处理中,-1表示失败
    private Integer code; 
    // 数据
    private Object data; 
    // 描述
    private String msg;

    public JsonData() {
    }

    public JsonData(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    // 成功,传入数据
    public static JsonData buildSuccess() {
        return new JsonData(0, null, null);
    }

    // 成功,传入数据
    public static JsonData buildSuccess(Object data) {
        return new JsonData(0, data, null);
    }

    // 失败,传入描述信息
    public static JsonData buildError(String msg) {
        return new JsonData(-1, null, msg);
    }

    // 失败,传入描述信息,状态码
    public static JsonData buildError(String msg, Integer code) {
        return new JsonData(code, null, msg);
    }

    // 成功,传入数据,及描述信息
    public static JsonData buildSuccess(Object data, String msg) {
        return new JsonData(0, data, msg);
    }

    // 成功,传入数据,及状态码
    public static JsonData buildSuccess(Object data, int code) {
        return new JsonData(code, data, null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg
                + "]";
    }

}

实体对象定义

package cn.xxx.xxx.mode;

import java.util.Date;

@Data
public class User {

    private int age;

    private String pwd;

    private String phone;

    private Date createTime;
}

工具类封装定义

package cn.xxx.xxx.utils;

import java.io.IOException;

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();

    //对象转字符串
    public static <T> String obj2String(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //字符串转对象
    public static <T> T string2Obj(String str,Class<T> clazz){
        if (StringUtils.isEmpty(str) || clazz == null){
            return null;
        }
        try {
            return clazz.equals(String.class)? (T) str :objectMapper.readValue(str,clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

redis工具类

package cn.xx.xx.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 功能描述:redis工具类
 *
 *
 */
@Component
public class RedisClient {


    //jdbcTemplate
    @Autowired
    private StringRedisTemplate redisTpl; 



    /**
     * 功能描述:设置key-value到redis中
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key ,String value){
        try{
            redisTpl.opsForValue().set(key, value);
            return true;
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }

    }


    /**
     * 功能描述:通过key获取缓存里面的值
     * @param key
     * @return
     */
    public String get(String key){

        return redisTpl.opsForValue().get(key);
    }
    /**
     * 通过redis操作设置map
     */
    public boolean setMap(String name,String key,Object value){
        try {
            redisTpl.opsForHash().put(name, key, value);
            return true;
        }catch (Exception e){
            return false;
        }
    }
    /**
     * 通过reids操作获取map
     */
    public Object getMap(String name,String key){
        Object result=redisTpl.opsForHash().get(name,key);
        return result;
    }


}

控制层编写

package cn.xx.xx.controller;


import java.util.Date;
import cn.hello.bike.mode.JsonData;
import cn.hello.bike.mode.User;
import cn.hello.bike.utils.JsonUtils;
import cn.hello.bike.utils.RedisClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/v1/redis")
public class RdisTestController {

    //jdbcTemplate
    @Autowired
    private StringRedisTemplate redisTpl; 

    @Autowired
    private RedisClient redis;

    @GetMapping(value="add")
    public Object add(){

        //redisTpl.opsForValue().set("name", "xdclass2018");
        redis.set("username", "xxxx");
        return JsonData.buildSuccess();

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值