溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java如何寫接口給別人調(diào)用的示例代碼

發(fā)布時(shí)間:2020-10-15 10:32:47 來(lái)源:腳本之家 閱讀:282 作者:greatkendy123 欄目:編程語(yǔ)言

計(jì)算機(jī)語(yǔ)言分類有很多,如C、C++、C#、Java、Php、Python等等,她們有各自的特性及擅長(zhǎng)的領(lǐng)域,但她們各自又不是全能的。在一個(gè)稍微大型一點(diǎn)的項(xiàng)目都會(huì)用到多種語(yǔ)言共同完成,那么這些編程語(yǔ)言如何進(jìn)行通信呢。什么意思呢,就是比如說(shuō)我Java寫的一個(gè)方法,其他編程語(yǔ)言要怎么去調(diào)用呢?這就是本文要探討的問(wèn)題了。

一般來(lái)說(shuō),方法層面的編程語(yǔ)言通信用的是網(wǎng)絡(luò)接口形式,只暴露出形參和結(jié)果供別人調(diào)用。接口一般分為接口定義者和接口調(diào)用者,定義者可以規(guī)定接收參數(shù)的類型及返回形式,而接口定義者則只能完全按照接口定義者規(guī)定的參數(shù)進(jìn)行訪問(wèn)。就叫是我們所說(shuō)的webService(網(wǎng)絡(luò)服務(wù))。

以前的做法是利用XML作接口格式定義,然后通過(guò)Http做通訊和請(qǐng)求,如大名鼎鼎的SOAP,其實(shí)現(xiàn)在也是的,只不過(guò)現(xiàn)在流行RestFul風(fēng)格的Rest接口形式,但用的還是XML+HTTP,那這兩者有啥區(qū)別呢?最大的區(qū)別就是SOAP返回的主要是XML格式,有時(shí)還需要附帶一些輔助文件,而Rest則還可以返回JSON類型的字符串,減少了很多繁亂的XML標(biāo)簽。本文就以Java為例,用她寫一個(gè)接口,并讓其他人去調(diào)用,以此來(lái)簡(jiǎn)單展示接口調(diào)用是怎么樣一個(gè)過(guò)程。

步驟:

1、本機(jī)裝有Java JDK運(yùn)行環(huán)境及編程IDE(如Myeclipse)

2、建立一個(gè)maven項(xiàng)目,用以下載jar包,項(xiàng)目結(jié)構(gòu)如下:

java如何寫接口給別人調(diào)用的示例代碼

3 pom.xml文件內(nèi)容如下:

<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內(nèi)容如下:

<?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則主要配置采用的容器、日志管理及攔截器,攔截器可以實(shí)現(xiàn)到達(dá)請(qǐng)求鄭濤之前進(jìn)行身份認(rèn)證,踢除沒(méi)有訪問(wèn)資格的請(qǐng)求,而rest項(xiàng)目一般是用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;
 
/**
 * 攔截請(qǐng)求
 */
@Provider
public class MyFilter implements ContainerRequestFilter {
 
 @Override
 public void filter(ContainerRequestContext context) throws IOException {
  
  //打印出所有請(qǐng)求頭
  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 注冊(cè)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() {
 
    //服務(wù)類所在的包路徑
    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風(fēng)格)

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 調(diào)用接口,有多種方式,本文用HttpClient

/**
   * 發(fā)送 post請(qǐng)求webService接口
   * @param url 訪問(wèn)的接口地址
   * @param account 用戶名
   * @param code 密碼
   * @param param 查詢參數(shù)
   * @return HttpResponse 該類包含請(qǐng)求方法的態(tài)碼及返回的數(shù)據(jù)
   */ 
  public static HttpResponse post(String url,String account,String code,Map<String,Object> param) { 
    //創(chuàng)建HttpClient實(shí)例及Post方法
  	HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 
    //對(duì)用戶密碼MD5加密
		code = getMD5(code);
		//添加相關(guān)請(qǐng)求頭,依情況而定
		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"); 
		//傳遞請(qǐng)求參數(shù)
		String paramJsonStr = JSON.toJSONString(param);
		if(param != null)
			httppost.setEntity(new StringEntity(paramJsonStr, Charset.forName("UTF-8"))); 
		HttpResponse response = null;
		try { 
			//執(zhí)行post請(qǐng)求
      response = httpclient.execute(httppost); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return response;
  }

10 返回接口

java如何寫接口給別人調(diào)用的示例代碼

11 最后通過(guò)Map、List和Json工具對(duì)返回來(lái)的字面量進(jìn)行各種處理。

	/**
	 * 將從接口返回的JSON數(shù)據(jù)轉(zhuǎn)換為實(shí)體集合等待插入數(shù)據(jù)表
	 * 實(shí)體 IgmAccessControlChannelModel --> IGM_ACCESSCONTROL_CHANNEL
	 * 	  IgmStationDoorModel --> IGM_STATION_DOOR
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private Map<String,Object> getDeviceEntityList(String data){
		Map<String,Object> totalMap = new HashMap<>();
		//把以下三個(gè)結(jié)果放到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 = ""; //用于設(shè)置igmaccessControlchannel表和igmstationDoor表的stationDoorid
			//判斷該門禁設(shè)備ID在本平臺(tái)是否匹配
			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不匹配的跳過(guò)
			}
		}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI