E9对接NCCRest接口

对接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请求接口需要去配置文件中设置白名单

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值