前言
在泛微系统上开发一个自定义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
测试成功,其他的功能大家可以试试