零基础泛微二开指南

前言

在泛微系统上开发一个自定义post接口

准备

首先准备工作要做好,安装一个泛微,之后所有的操作要在泛微的安装目录操作
参考官网安装,挺麻烦的;

IDEA

1、直接新建项目

new ->Project from Existing Sources.

直接打开泛微安装路径
在这里插入图片描述

2、总目录新建src目录

新的代码都开发在这个目录下
在这里插入图片描述

3、设置项目结构

·
在这里插入图片描述
使用泛微安装使用的自带JDK,没有用自己的也行,保持同环境作用

在这里插入图片描述

在这里插入图片描述

4、修改打包输出路径

修改目录D:\soft\fanwei\ecology\classbean 为自己打包编译路径
在这里插入图片描述
在源代码中可以看到此目录变为黄色
在这里插入图片描述

5、修改源代码目录

目录右键设置src为源目录,为蓝色
在这里插入图片描述

6、设置依赖

在这里插入图片描述

D:\soft\fanwei\ecology\WEB-INF\lib

classbean这个依赖可以没有,先引入这两个依赖,都是java类型,相当于jar
在这里插入图片描述

2、开发

1、目录

开发目录与正常spring一样,只不过controller叫了action,不叫也不影响使用

com.api.action 定义接口总路径
com.engine.action 则为实现路径 嫌麻烦一个类写完也可以,没啥固定要求

在这里插入图片描述

2、代码实例

如现有需求:做一个请求转发接口,带basic auth 认证的接口
UserInfoAction

 package com.api.action;

import javax.ws.rs.Path;

@Path("/userInfo")
public class UserInfoAction extends com.engine.action.UserInfoAction {
}

UserInfoAction
@Slf4j,IDEA需要支持插件,否则会打包失败

 package com.engine.action;

import com.engine.action.utils.PostMethodUtils;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

@Slf4j
public class UserInfoAction {
    /**
     * 人员信息获取接口
     * @param jsonObject
     * @return
     */
    @Path("/getSalaryByInfo")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Object getSalaryByInfo(@RequestBody JSONObject jsonObject){

        //获取发送URL
        String url = jsonObject.getString("url");
        String userName = jsonObject.getString("userName");
        String password = jsonObject.getString("password");
        if(url==""||url==null||userName==null||password==null){
            jsonObject.put("msg","发送参数有误,检查URL、userName、password");
            return jsonObject;
        }
        //移除参数
        jsonObject.remove("url");
        jsonObject.remove("userName");
        jsonObject.remove("password");
        log.info("发送参数:"+jsonObject);
        //发送
        PostMethodUtils postMethodUtils = new PostMethodUtils();
        String post = postMethodUtils.sendPost(url, jsonObject.toString(), userName, password);
        if(post==null){
            jsonObject.put("msg","返回信息为空");
            return jsonObject;
        }
        //格式化
        JSONObject returnMsg = JSONObject.fromObject(post);
        //获取解密 对应四个接口 只有O_RESULT解密
        Object o_result = returnMsg.get("O_RESULT");
        if(o_result!=null){
            byte[]  encodedString = Base64.getDecoder().decode(o_result.toString().getBytes());
            try {
                //解码后指定编码
                returnMsg.put("O_RESULT", new String(encodedString,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                log.info("报错信息:"+e.getMessage());
                throw new RuntimeException(e);
            }
        }
        log.info("返回参数:"+returnMsg);
        return returnMsg;
    }
    @Path("/getSalaryByInfo2")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object getSalaryByInfo2(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url","https://baidu.com");
        jsonObject.put("I_PAYTY","1");
        jsonObject.put("I_ABKRS","L1");
        jsonObject.put("I_INPER","202406");
        jsonObject.put("I_ZHRXZLX","10");
        jsonObject.put("I_ZBPMNO","OA00000000000001");
        jsonObject.put("userName","ifuser");
        jsonObject.put("password","123456");
        //获取发送URL
        String url = jsonObject.getString("url");
        String userName = jsonObject.getString("userName");
        String password = jsonObject.getString("password");
        if(url==""||url==null||userName==null||password==null){
            jsonObject.put("msg","发送参数有误,检查URL、userName、password");
            return jsonObject;
        }
        //移除参数
        jsonObject.remove("url");
        jsonObject.remove("userName");
        jsonObject.remove("password");
//        baseBean.writeLog("发送参数:"+jsonObject);
        log.info("发送参数:"+jsonObject);
        //发送
        PostMethodUtils postMethodUtils = new PostMethodUtils();
        String post = postMethodUtils.sendPost(url, jsonObject.toString(), userName, password);
        if(post==null){
            jsonObject.put("msg","返回信息为空");
            return jsonObject;
        }
        //格式化
        JSONObject returnMsg = JSONObject.fromObject(post);
        //获取解密
        //获取解密 对应四个接口 只有O_RESULT解密
        Object o_result = returnMsg.get("O_RESULT");
        if(o_result!=null){
            byte[]  encodedString = Base64.getDecoder().decode(o_result.toString().getBytes());
            try {
                //解码后指定编码
                returnMsg.put("O_RESULT", new String(encodedString,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                log.info("报错信息:"+e.getMessage());
                throw new RuntimeException(e);
            }
        }
        log.info("返回参数:"+returnMsg);
        return returnMsg;
    }
}

PostMethodUtils

 package com.engine.action.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Base64;
@Slf4j
public class PostMethodUtils {
 
    /**
     * 发送HttpClient请求
     * @param requestUrl
     * @param params
     * @return
     */
    public static String sendPost(String requestUrl, String params,String userName,String password ) {
        InputStream inputStream = null;
        try {
            HttpClient httpClient = new HttpClient();
            PostMethod postMethod = new PostMethod(requestUrl);
            // 设置请求头  Content-Type
            postMethod.setRequestHeader("Content-Type", "application/json");
            //Base64加密方式认证方式下的basic auth HAIN460000:用户名    topicis123:密码
            postMethod.setRequestHeader("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((userName + ":" + password).getBytes()));
            RequestEntity requestEntity = new StringRequestEntity(params,"application/json","UTF-8");
            postMethod.setRequestEntity(requestEntity);
            log.info("发送参数2:"+requestEntity);
            httpClient.executeMethod(postMethod);// 执行请求
            inputStream =  postMethod.getResponseBodyAsStream();// 获取返回的流
            BufferedReader br = null;
            StringBuffer buffer = new StringBuffer();
            // 将返回的输入流转换成字符串
            br = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String temp;
            while ((temp = br.readLine()) != null) {
                buffer.append(temp);
            }
            return buffer.toString();
        } catch (Exception e){
            log.info("报错信息:"+e.getMessage());
            throw new RuntimeException(e.getMessage());
        } finally {
            if(inputStream != null) {
                try{
                    inputStream.close();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

3、打包

直接项目右键打包编译

在这里插入图片描述
此时自定义输出目录就会有结果,这个目录泛微启动会加载二开内容
在这里插入图片描述

3、重启服务

重启服务、启动服务Resin9即可,看电脑配置,可能要十分钟,启动好了泛微会自动刷新登录页面

在这里插入图片描述

4、测试

因为我开发了一个GET一个post请求,涉及泛微认证,所以最简便做法是登录后,直接地址栏请求
注意路径要加api

localhost/api/userInfo/getSalaryByInfo2

在这里插入图片描述
测试成功,其他的功能大家可以试试
在这里插入图片描述

5、部署

部署也是很简单,找到要部署的泛微,在classbean目录下把你写的代码解压放进去就行,如果目录存在,就使用,把文件放到指定目录下,重启Resin即可

泛微安装目录

X:\ecology\classbean

查看目录

如果无com目录则直接将解压的com目录拖入,如果有,则在对应目录上传对应文件,一共三个
在这里插入图片描述

目录复刻

遵从原则:无此目录则直接拖入同级目录,有则点击

D:\soft\fanwei\ecology\classbean\com\engine\action

在这里插入图片描述

检查

确定三个class在指定目录都有

D:\ecology\classbean\com\api\action

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值