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

网友投稿 290 2022-12-28


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

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

一般来说,方法层面的编程语言通信用的是网络接口形式,只暴露出形参和结果供别人调用。接口一般分为接口定义者和接口调用者,定义者可以规定接收参数的类型及返回形式,而接口定义者则只能完全按照接口定义者规定的参数进行访问。就叫是我们所说的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文件内容如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

ws

restful

war

0.0.1-SNAPSHOT

&QNmLfKqHXqlt;name>restful-server

http://maven.apache.org

UTF-8

2.6

2.23.1

4.2.7.RELEASE

3.1.0

1.1.1

1.7.6

junit

junit

3.8.1

test

org.glassfish.jersey.ext

jersey-spring3

${jersey-spring.version}

org.springframework

spring-core

org.springframework

spring-web

org.springframework

spring-beans

org.glassfish.jersey.containers

jersey-container-servlet

${jersey-spring.version}

org.glassfish.jersey.containers

jersey-container-servlet-core

${jersey-spring.version}

org.glassfish.jersey.media

jersey-media-json-jackson

${jersey.version}

org.glassfish.jersey.media

jersey-media-multipart

${jersey.version}

org.glassfish.jersey.ext

jersey-entity-filtering

${jersey.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-test

${spring.version}

test

javax.servlet

javax.servlet-api

${servlet-api-version}

provided

ch.qos.logback

logback-classic

${logback.version}

org.slf4j

jcl-over-slf4j

${jcloverslf4j.version}

restful

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

ws

restful

war

0.0.1-SNAPSHOT

&QNmLfKqHXqlt;name>restful-server

http://maven.apache.org

UTF-8

2.6

2.23.1

4.2.7.RELEASE

3.1.0

1.1.1

1.7.6

junit

junit

3.8.1

test

org.glassfish.jersey.ext

jersey-spring3

${jersey-spring.version}

org.springframework

spring-core

org.springframework

spring-web

org.springframework

spring-beans

org.glassfish.jersey.containers

jersey-container-servlet

${jersey-spring.version}

org.glassfish.jersey.containers

jersey-container-servlet-core

${jersey-spring.version}

org.glassfish.jersey.media

jersey-media-json-jackson

${jersey.version}

org.glassfish.jersey.media

jersey-media-multipart

${jersey.version}

org.glassfish.jersey.ext

jersey-entity-filtering

${jersey.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-test

${spring.version}

test

javax.servlet

javax.servlet-api

${servlet-api-version}

provided

ch.qos.logback

logback-classic

${logback.version}

org.slf4j

jcl-over-slf4j

${jcloverslf4j.version}

restful

4 applicationContext.xml内容如下:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.1.xsd">

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

contextConfigLocation

classpath:applicationContext.xml

org.springframework.web.context.ContextLoaderListener

jersey-serlvet

org.glassfish.jersey.servlet.ServletContainer

QNmLfKqHXq javax.ws.rs.Application

com.kendy.filter.RestJaxRsApplication

1

jersey-serlvet

/*

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 map = context.getHeaders();

for(Entry> entry : map.entrySet()){

String key = entry.getKey();

List 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.PQNmLfKqHXqath;

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

/**

* 发送 post请求webService接口

* @param url 访问的接口地址

* @param account 用户名

* @param code 密码

* @param param 查询参数

* @return HttpResponse 该类包含请求方法的态码及返回的数据

*/

public static HttpResponse post(String url,String account,String code,Map 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 getDeviceEntityList(String data){

Map totalMap = new HashMap<>();

//把以下三个结果放到totalMap里返回

List accessControlList = new ArrayList<>();

List doorList = new ArrayList<>();

List deviceIds = new ArrayList<>();

IgmAccessControlChannelModel accessControlModel = null;

IgmStationDoorModel stationDoorModel = null;

Map map = JSON.parseObject(data, Map.class);

map = (Map>>)map;

List> deviceList = (List>) map.get("Device");

for(Map deviceMap : deviceList){

String deviceId = (String)(deviceMap.get("DeviceID")+"");

Object channels = deviceMap.get("Channels");

List> channelList = (List>)channels;

String id = ""; //用于设置igmaccessControlchannel表和igmstationDoor表的stationDoorid

//判断该门禁设备ID在本平台是否匹配

boolean isMatched = isDeviceIdMatched(deviceId);

if(isMatched){

for(Map chnnelMap : channelList){

accessControlModel = new IgmAccessControlChannelModel();

stationDoorModel = new IgmStationDoorModel();

for(Map.Entry 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不匹配的跳过

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信web 接口测试工具(微信检测接口)
下一篇:Java中Http连接的两种方式(小结)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~