Java调用SpringBoot接口实例

普通java项目引入的依赖


    cn.hutool
    hutool-all
    5.3.3

 java调用SpringBoot的接口实例

package com.atmk.stream;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * @author:lss
 * @date:2022/11/9 16:47
 * @description:some
 */
public class SpringBootInterfaceTest {
        sendData();

    }

    //调用spring boot中controller层的接口
    public static String sendData(){
        Map paramsMap= new HashMap();
        paramsMap.put("para","lss");
        paramsMap.put("bb","bbbb");
        String response = null;
        String sendMsg="";
        sendMsg= JSON.toJSONString(paramsMap);
        try {
            response = HttpUtil.post("http://localhost:52000/bizdm-service-lss/gateData/Test?token=eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiJhdXRvbWljIiwidXNlck5hbWUiOiJsaWFuZ2xpcWkiLCJ1c2VySWQiOjkyLCJ1c2VyTmlja05hbWUiOiJsaWFuZ2xpcWkiLCJleHAiOjE2Njg3NDQxNjAsIm5iZiI6MTY2ODc0MDU2MH0.Yp_OPWgC2P9ZfU0kKsEVkLOi_cRUxUcD-JWqOO2-x0g", paramsMap);//使用了hutool工具类 HttpUtil
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("===="+response);
        return response;
    }

 SpringBoot项目的接口代码

@RequestMapping(value = "Test", method = RequestMethod.POST)
    @ApiOperation(value = "测试")
    public BaseWrapper Test(
            @RequestParam Map para) {
        BaseWrapper baseWrapper = new BaseWrapper<>();
        try {
            baseWrapper.setEntity("09890");
            baseWrapper.setCode(200);
            System.out.println("para=" + JSON.toJSONString(para));
            baseWrapper.setMessage("查询成功===!" + para.get("para"));
        } catch (Exception e) {
            e.printStackTrace();
            baseWrapper.setCode(500);
            baseWrapper.setMessage("查询失败===!");
        }
        return baseWrapper;
    }

接口调用输出:

===={"code":200,"message":"查询成功===!lss","entity":"09890"} 

你可能感兴趣的:(java,spring,boot,json)