对接NCC
rest接口规范
-
两个不同系统之间的交互,通常时通过
URL
定位符去发送请求去调用按照另一系统规范完成的接口,就比如NCC的rest接口的开发是通过继承AbstractUAPRestResource
抽象类,并重写其中的getModule
方法。public abstract class AbstractUAPRestResource extends ServerResource implements IUAPRestResource { public AbstractUAPRestResource() { } //方法用来返回模块名 public abstract String getModule(); }
在继承后还需要使用注解去配置接口请求路径,通常是通过
@Path
注解来注解方法,调用时的路径例如http://ip:prot/uapws/rest/...
@POST @Path("insert") @Produces("application/json") @Consumes("application/json") public JSONString reqJSON(JSONString JSON)
再例如泛微E_cology对于外部接口的规范,就需要特别注意接口类的全包名需要再
com.api
路径下,调用的路径为http://ip:port/api/...
package com.api.prj.impl; import com.alibaba.fastjson.JSON; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; 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.Context; import javax.ws.rs.core.MediaType; /** * @author Sleep * @Create 2023/8/24 9:29 * @Note */ @Path("JerseyRest") public class WeaverJerseyRestImplTest { @POST @Path("post") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public String toPost(@Context HttpServletRequest request, @Context HttpServletResponse response){ return JSON.toJSONString("testPOST"); } @GET @Path("get") @Produces(MediaType.APPLICATION_JSON) public String toGet(@Context HttpServletRequest request, @Context HttpServletResponse response){ return JSON.toJSONString("testGET"); } }
上述GET请求能提交成功,而POST请求会失败
这需要在开发环境中设置白名单,开放不需要鉴权的接口。
https://e-cloudstore.com/doc.html?appId=7065b44c3d614b1d981fbd9cb395b6a2
设置后POST请求就会请求成功
调用rest接口
-
接口的调用实际上就是根据请求路径携带参数去发送请求,这个过程也需要根据规范来实现,毕竟要将请求的动作与系统流程的某个节点绑定,需要跟着规范来嘛。例如泛微的系统中就需要去继承BaseBean类与实现Action接口。
public class NCCProjectVOBaseAction extends BaseBean implements Action
并重写其中的execute方法
public String execute(RequestInfo requestInfo)
其中的参数
RequestInfo
封装着流程中的信息,可以通过get方法获取属性再进行数据处理封装:Property[] properties = requestInfo.getMainTableInfo().getProperty(); NCCProjectVO vo = new NCCProjectVO(); for (Property property : properties) { if (property.getName().equals("xmm")) vo.setProject_name(property.getValue()); else if (property.getName().equals("zzm")) vo.setPk_org(property.getValue()); else if (property.getName().equals("jtm")) vo.setPk_group(property.getValue()); else if (property.getName().equals("xmfl")) vo.setPk_eps(property.getValue()); else if (property.getName().equals("xmbm")) vo.setProject_code(property.getValue()); else if (property.getName().equals("xmqssj")) vo.setPlan_start_date(property.getValue()+" 00:00:00"); else if (property.getName().equals("xmzzsj")) vo.setPlan_finish_date(property.getValue()+" 00:00:00"); else if (property.getName().equals("qyzt")) vo.setEnablestate(Integer.valueOf(property.getValue())); }
-
发送POST请求:通常发送请求都是按照Rest规范发送和接收Rest数据,所以可以将方法封装为一个Utils类:
package com.servlet.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; /** * @author Sleep * @Create 2023/8/23 17:36 * @Note */ public class RestUtils { public static String restCallerPost(String url, String param) { int responseCode; String data = ""; try { URL restURL = new URL(url); sun.net.www.protocol.http.HttpURLConnection conn = (sun.net.www.protocol.http.HttpURLConnection) restURL.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); conn.setDoOutput(true); OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); os.write(param); os.flush(); responseCode = conn.getResponseCode(); if(responseCode == 200){ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); int line = 1; String tempString = null; while ((tempString = reader.readLine()) != null) { data += tempString; line++; } reader.close(); } else { data = "false"; } os.close(); conn.disconnect(); } catch (Exception e) { e.printStackTrace(); } return data; } }
通过调用封装好静态方法,就可以发送POST请求
public class NCCProjectVOBaseAction extends BaseBean implements Action { //NCC请求地址 private static final String NCC_PROJECT_URL = "http://127.0.0.1:8081/uapws/rest/service/restuapbd/insert"; @SneakyThrows @Override public String execute(RequestInfo requestInfo) { Property[] properties = requestInfo.getMainTableInfo().getProperty(); NCCProjectVO vo = new NCCProjectVO(); for (Property property : properties) { if (property.getName().equals("xmm")) vo.setProject_name(property.getValue()); else if (property.getName().equals("zzm")) vo.setPk_org(property.getValue()); else if (property.getName().equals("jtm")) vo.setPk_group(property.getValue()); else if (property.getName().equals("xmfl")) vo.setPk_eps(property.getValue()); else if (property.getName().equals("xmbm")) vo.setProject_code(property.getValue()); else if (property.getName().equals("xmqssj")) vo.setPlan_start_date(property.getValue()+" 00:00:00"); else if (property.getName().equals("xmzzsj")) vo.setPlan_finish_date(property.getValue()+" 00:00:00"); else if (property.getName().equals("qyzt")) vo.setEnablestate(Integer.valueOf(property.getValue())); } System.out.println(properties.toString()); String json = JSONObject.toJSON(vo).toString(); String relustStr = RestUtils.restCallerPost(NCC_PROJECT_URL,json); System.out.println(relustStr); return SUCCESS; } }
-
同理在NCC
中也可以通过同样的方式去调用泛微的接口,唯一需要区别的就是泛微中的POST请求接口需要去配置文件中设置白名单
。