java如何写接⼝给别⼈调⽤
计算机语⾔分类有很多,如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包,项⽬结构如下:
l⽂件内容如下:
1<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
2 xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>ws</groupId>
5 <artifactId>restful</artifactId>
6 <packaging>war</packaging>
7 <version>0.0.1-SNAPSHOT</version>
8 <name>restful-server</name>
9 <url></url>
10 <properties>
11 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12 <jersey.version>2.6</jersey.version>
13 <jersey-spring.version>2.23.1</jersey-spring.version>
14 <spring.version>4.2.7.RELEASE</spring.version>
15 <servlet-api-version>3.1.0</servlet-api-version>
16 <logback.version>1.1.1</logback.version>
17 <jcloverslf4j.version>1.7.6</jcloverslf4j.version>
18 </properties>
19 <dependencies>
20 <dependency>
21 <groupId>junit</groupId>
22 <artifactId>junit</artifactId>
23 <version>3.8.1</version>
24 <scope>test</scope>
24 <scope>test</scope>
25 </dependency>
26
27 <dependency>
28 <groupId>org.</groupId>
29 <artifactId>jersey-spring3</artifactId>
30 <version>${jersey-spring.version}</version>
31 <exclusions>
32 <exclusion>
33 <groupId>org.springframework</groupId>
34 <artifactId>spring-core</artifactId>
35 </exclusion>
36 <exclusion>
37 <groupId>org.springframework</groupId>
38 <artifactId>spring-web</artifactId>
39 </exclusion>
40 <exclusion>
41 <groupId>org.springframework</groupId>
42 <artifactId>spring-beans</artifactId>
43 </exclusion>
44 </exclusions>
45 </dependency>
46
47 <dependency>
48 <groupId>org.ainers</groupId>
49 <artifactId>jersey-container-servlet</artifactId>
50 <version>${jersey-spring.version}</version>
51 </dependency>
52 <dependency>
53 <groupId>org.ainers</groupId>
54 <artifactId>jersey-container-servlet-core</artifactId>
55 <version>${jersey-spring.version}</version>
56 </dependency>
57
58 <dependency>
59 <groupId>org.dia</groupId>
60 <artifactId>jersey-media-json-jackson</artifactId>
61 <version>${jersey.version}</version>
62 </dependency>
63 <dependency>
64 <groupId>org.dia</groupId>
65 <artifactId>jersey-media-multipart</artifactId>
66 <version>${jersey.version}</version>
67 </dependency>
68 <dependency>
69 <groupId>org.</groupId>
70 <artifactId>jersey-entity-filtering</artifactId>
71 <version>${jersey.version}</version>
72 </dependency>
73
74 <!-- Spring4.2 dependencies -->
75 <dependency>
76 <groupId>org.springframework</groupId>
77 <artifactId>spring-core</artifactId>
78 <version>${spring.version}</version>
79 </dependency>
80
81 <dependency>
82 <groupId>org.springframework</groupId>
83 <artifactId>spring-context</artifactId>
84 <version>${spring.version}</version>
85 </dependency>
86
87 <dependency>
88 <groupId>org.springframework</groupId>
89 <artifactId>spring-web</artifactId>
89 <artifactId>spring-web</artifactId>
90 <version>${spring.version}</version>
91 </dependency>
92
93 <dependency>
94 <groupId>org.springframework</groupId>
95 <artifactId>spring-jdbc</artifactId>
96 <version>${spring.version}</version>
97 </dependency>
98
99 <dependency>
100 <groupId>org.springframework</groupId>
101 <artifactId>spring-tx</artifactId>
102 <version>${spring.version}</version>
103 </dependency>
104 <dependency>
105 <groupId>org.springframework</groupId>
106 <artifactId>spring-test</artifactId>
107 <version>${spring.version}</version>
108 <scope>test</scope>
109 </dependency>
110
111 <dependency>
112 <groupId>javax.servlet</groupId>
113 <artifactId>javax.servlet-api</artifactId>
114 <version>${servlet-api-version}</version>
115 <scope>provided</scope>
116 </dependency>
117
118 <!-- Logback dependencies -->
119 <dependency>
120 <groupId>ch.qos.logback</groupId>
121 <artifactId>logback-classic</artifactId>
122 <version>${logback.version}</version>
123 </dependency>
124 <dependency>
125 <groupId>org.slf4j</groupId>
126 <artifactId>jcl-over-slf4j</artifactId>
127 <version>${jcloverslf4j.version}</version>
128 </dependency>
129
130 </dependencies>
131 <build>
132 <finalName>restful</finalName>
133 </build>
134</project>
l内容如下:
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="/schema/beans"
3 xmlns:xsi="/2001/XMLSchema-instance"
4 xmlns:context="/schema/context"
5 xmlns:tx="/schema/tx"
6 xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd
7 /schema/context /schema/context/spring-context-4.1.xsd">
8
9 <context:component-scan base-package="com.kendy.*"></context:component-scan>
10</beans>
l则主要配置采⽤的容器、⽇志管理及,可以实现到达请求郑涛之前进⾏⾝份认证,踢除没有访问资格的请求,⽽rest项⽬⼀般是⽤Jersey容器。配置如下:
java怎么编写(kendy只是我的英⽂名,你们可以⾃⼰定义路径)
1<?xml version="1.0" encoding="UTF-8"?>
2<web-app xmlns:javaee="java.sun/xml/ns/javaee" xmlns:web="java.sun/xml/ns/javaee/web-app_2_5.xsd">
3 <display-name></display-name>
4 <context-param>
5 <param-name>contextConfigLocation</param-name>
6 <param-value>l</param-value>
7 </context-param>
8 <listener>
9 <listener-class>
10 org.t.ContextLoaderListener
11 </listener-class>
12 </listener>
13 <servlet>
14 <servlet-name>jersey-serlvet</servlet-name>
15 <servlet-class>
16 org.glassfish.jersey.servlet.ServletContainer
17 </servlet-class>
18 <init-param>
19 <param-name>javax.ws.rs.Application</param-name>
20 <param-value>com.kendy.filter.RestJaxRsApplication</param-value>
21 </init-param>
22 <load-on-startup>1</load-on-startup>
23 </servlet>
24 <servlet-mapping>
25 <servlet-name>jersey-serlvet</servlet-name>
26 <url-pattern>/*</url-pattern>
27 </servlet-mapping>
28</web-app>
6 如下:
1import java.io.IOException;
2import java.io.InputStream;
3import java.util.List;
4import java.util.Map.Entry;
5
6import javax.ws.rs.WebApplicationException;
7import javax.ainer.ContainerRequestContext;
8import javax.ainer.ContainerRequestFilter;
9import javax.MultivaluedMap;
10import javax.Response.Status;
11import javax.Provider;
12
13/**
14 * 拦截请求
15 */
16@Provider
17public class MyFilter implements ContainerRequestFilter {
18
19 @Override
20 public void filter(ContainerRequestContext context) throws IOException { 21
22 //打印出所有请求头
23 MultivaluedMap<String, String> map = Headers();
24 for(Entry<String, List<String>> entry : Set()){
25 String key = Key();
26 List<String> valueList = Value();
27 String values = String();
28 System.out.println(key + ":"+values.substring(1,values.length()-1));
29 }
7 注册Jersey容器所要管理的类
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论