java如何写接口给别人调用?实例介绍

我们应该知道,写接口就是为了方便程序员开发的,这样也节省程序员的时间,那么java如何写接口给别人调用?接下来,我们就来给大家讲解一下这方面的内容。

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

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

java如何写接口给别人调用?实例介绍.jpg

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内容如下:

<beans xmlns="http://www.springframework.org/schema/beans" p="" <="">
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.*">

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
         {
             //打印出所有请求头
             MultivaluedMapmap = context.getHeaders();
             for (Entry < string, list > entry: map.entrySet())
             {
                 String key = entry.getKey();
                 ListvalueList = 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风格)

[java] view plain copy
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 size()
    {
        Map 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 >> getDetail()
    {
        Map >> map = new HashMap < > ();
        //do something here
        List > list = new ArrayList < > ();
        Map 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

[java] view plain copy
/** 
 * 发送 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 返回接口

1.jpg

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

[java] view plain copy
/** 
 * 将从接口返回的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不匹配的跳过  
            }
        }

这些就是java写接口给别人调用的实例教程,大家可以参考这些方法去写接口程序。最后大家如果想要了解更多java实例知识,敬请关注奇Q工具网。

推荐阅读:

java程序怎么输出一串整数?java输出方式有哪些?

java软件怎么改中文?Java有哪些好用的软件?

json文件怎么修改?json有哪些形式?