springboot 封装接口请求结果

实现步骤:

一、添加业务码类

public class ApiCodeConstant {
    public static final int RESULT_SUCC = 20000; //接口请求成功

    public static final int RESULT_WARN = 40000; //接口请求警告

    public static final int RESULT_ERROR = 50000; //接口请求错误

    public static final int RESULT_LOGIN_FAIL = 50001;  //用户名或者密码错误
    public static final int RESULT_PERMISSION_EXCEPTION= 50002;         //自定义权限异常
    public static final int RESULT_TOKEN_FAIL = 50014;         //token校验失败
    public static final int RESULT_NULLPOINT_EXCEPTION= 50101;         //空指针异常
    public static final int RESULT_OTHER_EXCEPTION= 50102;         //其他异常

    public static final int RESULT_ID_GEN_EXCEPTION= 502021;         //id派发异常

    public static final String MSG_ID_GEN_EXCEPTION= "ID生成异常";         //id派发异常消息

    public static final int PARAMETER_VALIDATION_ERROR= 50103;         //其他异常
}

二、添加请求结果封装类

public class ApiResult extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    public static final String CODE_TAG = "code";

    /** 返回内容 */
    public static final String MSG_TAG = "msg";

    /** 数据对象 */
    public static final String DATA_TAG = "data";


    /**
     * 构建空信息对象
     */
    public ApiResult(){

    }

    /**
     * 初始化一个新创建的 AjaxResult 对象
     *
     * @param code 状态码
     * @param msg 返回内容
     * @param data 数据对象
     */
    public ApiResult(int code, String msg, Object data)
    {
        super.put(CODE_TAG, code);
        super.put(MSG_TAG, msg);
        if (data != null)
        {
            super.put(DATA_TAG, data);
        }
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static ApiResult success()
    {
        return ApiResult.success("操作成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static ApiResult success(Object data)
    {
        return ApiResult.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static ApiResult success(String msg)
    {
        return ApiResult.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static ApiResult success(String msg, Object data)
    {
        return new ApiResult(ApiCodeConstant.RESULT_SUCC, msg, data);
    }

    /**
     * 返回警告消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static ApiResult warn(String msg)
    {
        return ApiResult.warn(msg, null);
    }

    /**
     * 返回警告消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static ApiResult warn(String msg, Object data)
    {
        return new ApiResult(ApiCodeConstant.RESULT_WARN, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @return 错误消息
     */
    public static ApiResult error()
    {
        return ApiResult.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 错误消息
     */
    public static ApiResult error(String msg)
    {
        return ApiResult.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @param data 数据对象
     * @return 错误消息
     */
    public static ApiResult error(String msg, Object data)
    {
        return new ApiResult(ApiCodeConstant.RESULT_ERROR, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg 返回内容
     * @return 错误消息
     */
    public static ApiResult error(int code, String msg)
    {
        return new ApiResult(code, msg, null);
    }


    /**
     * 是否为成功消息
     *
     * @return 结果
     */
    public boolean isSuccess()
    {
        return Objects.equals(ApiCodeConstant.RESULT_SUCC, this.get(CODE_TAG));
    }

    /**
     * 是否为警告消息
     *
     * @return 结果
     */
    public boolean isWarn()
    {
        return Objects.equals(ApiCodeConstant.RESULT_WARN, this.get(CODE_TAG));
    }

    /**
     * 是否为错误消息
     *
     * @return 结果
     */
    public boolean isError()
    {
        return Objects.equals(ApiCodeConstant.RESULT_ERROR, this.get(CODE_TAG));
    }

    /**
     * 方便链式调用
     *
     * @param key 键
     * @param value 值
     * @return 数据对象
     */
    @Override
    public ApiResult put(String key, Object value)
    {
        super.put(key, value);
        return this;
    }



}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot封装HTTP请求可以使用RestTemplate或者Feign来实现。下面分别介绍这两种方式的使用: 1. 使用RestTemplate: RestTemplate是Spring提供的一个HTTP客户端工具,可以发送HTTP请求并处理响应。以下是一个简单的封装示例: ```java import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; public class HttpUtils { private static final RestTemplate restTemplate = new RestTemplate(); public static <T> T sendGetRequest(String url, Class<T> responseType) { HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(headers); ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, responseType); return responseEntity.getBody(); } public static <T> T sendPostRequest(String url, Object request, Class<T> responseType) { HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); HttpEntity<Object> entity = new HttpEntity<>(request, headers); ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, responseType); return responseEntity.getBody(); } } ``` 上述代码中,我们定义了一个HttpUtils类,提供了sendGetRequest和sendPostRequest两个方法,分别用于发送GET和POST请求,并且可以指定返回结果的类型。 2. 使用Feign: Feign是一个声明式的Web服务客户端,可以通过注解方式定义和实现HTTP请求。以下是一个简单的封装示例: 1)首先,在Spring Boot的启动类上添加@EnableFeignClients注解: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 2)然后,创建一个Feign客户端接口: ```java import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @FeignClient(name = "example", url = "http://example.com") public interface HttpClient { @GetMapping("/api/get") String get(); @PostMapping("/api/post") String post(@RequestBody Object request); } ``` 在上述代码中,我们使用@FeignClient注解指定了服务名和请求URL。 3)最后,在需要使用HTTP请求的地方注入HttpClient接口并调用相应的方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final HttpClient httpClient; @Autowired public MyService(HttpClient httpClient) { this.httpClient = httpClient; } public String doHttpRequest() { String response = httpClient.get(); // 处理响应 return response; } } ``` 在上述代码中,我们通过@Autowired注解将HttpClient接口注入到MyService中,并调用需要的方法来发送HTTP请求。 以上是在Spring Boot封装HTTP请求的两种常用方式,你可以根据具体需求选择使用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值