java如何写接口给别人调用的示例代码

29 2019-9-24 00:12

计算机语言分类有很多,如C、C++、C#、Java、Php、Python等等,她们有各自的特性及擅长的领域,但她们各自又不是全能的。在一个稍微大型一点的项目都会用到多种语言共同完成,那么这些编程语言如何进行通信呢。什么意思呢,就是比如说我Java写的一个方法,其他编程语言要怎么去调用呢?这就是本文要探讨的问题了。

一般来说,方法层面的编程语言通信用的是网络接口形式,只暴露出形参和结果供别人调用。接口一般分为接口定义者和接口调用者,定义者可以规定接收参数的类型及返回形式,而接口定义者则只能完全按照接口定义者规定的参数进行访问。就叫是我们所说的webService(网络服务)。

以前的做法是利用XML作接口格式定义,然后通过Http做通讯和请求,如大名鼎鼎的SOAP,其实现在也是的,只不过现在流行RestFul风格的Rest接口形式,但用的还是XML+HTTP,那这两者有啥区别呢?最大的区别就是SOAP返回的主要是XML格式,有时还需要附带一些辅助文件,而Rest则还可以返回JSON类型的字符串,减少了很多繁乱的XML标签。本文就以Java为例,用她写一个接口,并让其他人去调用,以此来简单展示接口调用是怎么样一个过程。

步骤:

1、本机装有Java JDK运行环境及编程IDE(如Myeclipse)

2、建立一个maven项目,用以下载jar包,项目结构如下:

3 pom.xml文件内容如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>ws</groupId>
 <artifactId>restful</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>restful-server</name>
 <url>http://maven.apache.org</url>
 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jersey.version>2.6</jersey.version>
    <jersey-spring.version>2.23.1</jersey-spring.version>
    <spring.version>4.2.7.RELEASE</spring.version>
    <servlet-api-version>3.1.0</servlet-api-version>
    <logback.version>1.1.1</logback.version>
    <jcloverslf4j.version>1.7.6</jcloverslf4j.version>
  </properties>
 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
   
  <dependency>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-spring3</artifactId>
      <version>${jersey-spring.version}</version>
      <exclusions>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>${jersey-spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet-core</artifactId>
      <version>${jersey-spring.version}</version>
    </dependency>
  
     <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-json-jackson</artifactId>
      <version>${jersey.version}</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.media</groupId>
      <artifactId>jersey-media-multipart</artifactId>
      <version>${jersey.version}</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.ext</groupId>
      <artifactId>jersey-entity-filtering</artifactId>
      <version>${jersey.version}</version>
    </dependency>
  
    <!-- Spring4.2 dependencies -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>${servlet-api-version}</version>
      <scope>provided</scope>
    </dependency>
  
    <!-- Logback dependencies -->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>${logback.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>jcl-over-slf4j</artifactId>
      <version>${jcloverslf4j.version}</version>
    </dependency>
   
 </dependencies>
 <build>
  <finalName>restful</finalName>
 </build>
</project>

4 applicationContext.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  
  <context:component-scan base-package="com.kendy.*"></context:component-scan>
</beans>

5 web.xml则主要配置采用的容器、日志管理及拦截器,拦截器可以实现到达请求郑涛之前进行身份认证,踢除没有访问资格的请求,而rest项目一般是用Jersey容器。配置如下:(kendy只是我的英文名,你们可以自己定义路径)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></display-name>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>
      org.springframework.web.context.ContextLoaderListener
    </listener-class>
 </listener>
 <servlet>
  <servlet-name>jersey-serlvet</servlet-name>
  <servlet-class>
      org.glassfish.jersey.servlet.ServletContainer
    </servlet-class>
  <init-param>
   <param-name>javax.ws.rs.Application</param-name>
   <param-value>com.kendy.filter.RestJaxRsApplication</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>jersey-serlvet</servlet-name>
  <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>

6 拦截器如下:

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map.Entry;
  
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;
  
/**
 * 拦截请求
 */
@Provider
public class MyFilter implements ContainerRequestFilter {
  
 @Override
 public void filter(ContainerRequestContext context) throws IOException {
   
  //打印出所有请求头
  MultivaluedMap<String, String> map = context.getHeaders();
  for(Entry<String, List<String>> entry : map.entrySet()){
    String key = entry.getKey();
    List<String> valueList = entry.getValue();
    String values = valueList.toString();
    System.out.println(key + ":"+values.substring(1,values.length()-1));
  }

7 注册Jersey容器所要管理的类

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.glassfish.jersey.server.ResourceConfig;
  
import com.kendy.restful.AccountManager;
import com.kendy.restful.AuthorityManager;
import com.kendy.restful.CardManager;
import com.kendy.restful.DeviceManager;
import com.kendy.restful.UserResource;
  
/**
 * ${DESCRIPTION}
 */
public class RestJaxRsApplication extends ResourceConfig {
  
  /**
   * Register JAX-RS application components.
   */
  public RestJaxRsApplication() {
  
    //服务类所在的包路径
    packages("com.kendy.*");
     
    // register application resources
    this.register(YouClass.class);
    
    // register filters
    this.register(MyFilter.class);
    //this.register(RequestContextFilter.class);
     
    register(JacksonJsonProvider.class);
    //register(MultiPartFeature.class);
  }
}

8 接口定义(Restful风格)

package com.kendy.restful;
  
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
  
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
  
import org.springframework.stereotype.Component;
  
@Component
@Path("/CardManager")
public class CardManager {
     
  private SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  
  @POST
  @Path("size")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Map<String,Object> size(){
    Map<String,Object> map = new HashMap<>();
    //do something here
    map.put("Size", 1024);
    return map;
  }
   
  @POST
  @Path("getDetail")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Map<String,List<Map<String,Object>>> getDetail(){
    Map<String,List<Map<String,Object>>> map = new HashMap<>();
    //do something here
    List<Map<String,Object>> list = new ArrayList<>();
    Map<String,Object> subMap = new HashMap<>();
    subMap.put("UUID", 10086);
    subMap.put("CardID", 10081);
    subMap.put("StartTime", sdf.format(new Date()));
    subMap.put("EndTime", sdf.format(new Date()));
    list.add(subMap);
    map.put("Card", list);
     
    return map;
  }
}

9 调用接口,有多种方式,本文用HttpClient

/**
   * 发送 post请求webService接口
   * @param url 访问的接口地址
   * @param account 用户名
   * @param code 密码
   * @param param 查询参数
   * @return HttpResponse 该类包含请求方法的态码及返回的数据
   */
  public static HttpResponse post(String url,String account,String code,Map<String,Object> param) { 
    //创建HttpClient实例及Post方法
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 
    //对用户密码MD5加密
        code = getMD5(code);
        //添加相关请求头,依情况而定
        httppost.setHeader("Authorization","WSSE profile=\"UsernameToken\""); 
        httppost.setHeader("X-WSSE","UsernameToken Username=\""+account+"\",PasswordDigest=\""+code+"\""); 
        httppost.addHeader("Content-type","application/json; charset=utf-8"); 
        httppost.setHeader("Accept", "application/json"); 
        httppost.setHeader("Connection","keep-alive"); 
        httppost.setHeader("Accept-Encoding","gzip"); 
        httppost.setHeader("Accept-Language", "zh-CN,en,*"); 
        httppost.setHeader("User-Agent","Mozilla/5.0"); 
        //传递请求参数
        String paramJsonStr = JSON.toJSONString(param);
        if(param != null)
            httppost.setEntity(new StringEntity(paramJsonStr, Charset.forName("UTF-8"))); 
        HttpResponse response = null;
        try { 
            //执行post请求
      response = httpclient.execute(httppost); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return response;
  }

10 返回接口


11 最后通过Map、List和Json工具对返回来的字面量进行各种处理。

/**
 * 将从接口返回的JSON数据转换为实体集合等待插入数据表
 * 实体 IgmAccessControlChannelModel --> IGM_ACCESSCONTROL_CHANNEL
 *    IgmStationDoorModel --> IGM_STATION_DOOR
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private Map<String,Object> getDeviceEntityList(String data){
    Map<String,Object> totalMap = new HashMap<>();
    //把以下三个结果放到totalMap里返回
    List<IgmAccessControlChannelModel> accessControlList = new ArrayList<>();
    List<IgmStationDoorModel> doorList = new ArrayList<>();
    List<String> deviceIds = new ArrayList<>();
     
    IgmAccessControlChannelModel accessControlModel = null;
    IgmStationDoorModel stationDoorModel = null;
     
    Map map = JSON.parseObject(data, Map.class);
    map = (Map<String,List<Map<String,Object>>>)map;
    List<Map<String,Object>> deviceList = (List<Map<String, Object>>) map.get("Device");
     
    for(Map<String,Object> deviceMap : deviceList){
        String deviceId = (String)(deviceMap.get("DeviceID")+"");
        Object channels = deviceMap.get("Channels");
        List<Map<String,Object>> channelList = (List<Map<String,Object>>)channels;
         
        String id = ""; //用于设置igmaccessControlchannel表和igmstationDoor表的stationDoorid
        //判断该门禁设备ID在本平台是否匹配
        boolean isMatched = isDeviceIdMatched(deviceId);
         
        if(isMatched){
            for(Map<String,Object> chnnelMap : channelList){
                accessControlModel = new IgmAccessControlChannelModel();
                stationDoorModel = new IgmStationDoorModel();
                for(Map.Entry<String, Object> entry : chnnelMap.entrySet()){
                    String key = entry.getKey().trim();
                    String value = "";
                    if(key.equals("Channel")){
                        value =(Integer)entry.getValue()+"";
                    }else{
                        value = (String)entry.getValue();
                    }
                     
                    switch(key){
                    case "Channel":
                        accessControlModel.setCommandCode(Integer.parseInt(value));
                        accessControlModel.setControlChannelName("控制通道"+value);
                        stationDoorModel.setDoorCode(value);
                        break;
                    case "Description":
                        stationDoorModel.setStationDoorName(value);
                        break;
                    default: break;
                    }
                }
                id = UUID.randomUUID().toString();
                 
                accessControlModel.setAccessControlChannelId(UUID.randomUUID().toString());
                accessControlModel.setDeviceId(deviceId);
                accessControlModel.setStationDoorId(id);
                 
                stationDoorModel.setStationDoorId(id);
                stationDoorModel.setDeviceId(deviceId);
                stationDoorModel.setRemark("指纹门禁");
                 
                accessControlList.add(accessControlModel);
                doorList.add(stationDoorModel);
            }
            deviceIds.add(deviceId);
        }else{
            //deviceId不匹配的跳过
        }
    }