Java WebService记

Web Services开发

==============

常用的 Web Services 框架有 Apache Axis1 、 Apache Axis2 、 Apache CXF ,而 Apache Axis1 已经逐渐被淘汰所以本文不会讨论,重点关注 Apache Axis2 及 Apache CXF 。

Apache Axis2

============

在IDEA中新建 Axis2Demo 项目后右键选择 添加框架的支持 并选中 Web Application 。

Java WebService记

Java WebService记

从Apache Axis2官网处下载 war包 进行部署,将 axis2.war 解压后把 WEB-INF 和 axis2-web目录复制到项目的 web 目录下(如下图所示)并启动Tomcat Server。

Java WebService记

Java WebService记

访问

http://localhost:8080/Axis2Demo_war_exploded/axis2-web/index.jsp 出现下图的页面表示部署成功。

Java WebService记

Java WebService记

Axis2配置

=======

在 Axis1 中的全局配置和 Servcies 的配置均在 server-config.wsdd 中进行配置,而 Axis2则将全局配置单独存放于 WEB-INF/conf/axis2.xml 中, services 的配置文件则位于 servcies。

发布服务(Publish Service)

=====================

新建一个 HelloService 类并编译为 HelloService.class 复制至 WEB-INF/pojo 目录下并重启服务。

// 不能声明package

public class HelloService {

public HelloService(){}

public String sayHello() {

return “hello”;

}

public String sayHelloToPerson(String name) {

if (name == null) {

name = “nobody”;

}

return "hello, " + name;

}

}

重启服务后再次访问

http://localhost:8080/Axis2Demo_war_exploded/services/HelloService?wsdl 即可发现新发布的服务,点击 HelloService 即可查看Axis自动为该服务生成的WSDL,其描述了如何调用服务的方法及返回内容:

Java WebService记

Java WebService记

Java WebService记

使用 SoapUI 客户端调用 HelloService 服务方法:

Java WebService记

Java WebService记

而之所以 WEB-INF/pojo 目录下的 .class 文件会自动发布为服务是因为在 axis2.xml 配置文件中的 deployer 标签中所配置的该选项。

上述的方式发布服务需要将编译后的类放置在某个具体的目录中,且不能包含 package ,而使用 *.aar 的方式则可以解决此问题。首先在Project的根目录下新建 META-INF/services.xml ,文件内容可以参考官方示例 version.aar 。

<?xml version="1.0" encoding="UTF-8"?>

一个简单的WebService

com.ws.test.services.HelloService

<messageReceiver mep=“http://www.w3.org/ns/wsdl/in-only”

class=“org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />

<messageReceiver mep=“http://www.w3.org/2004/08/wsdl/in-out”

class=“org.apache.axis2.rpc.receivers.RPCMessageReceiver” />

最终结构为如下所示,在项目根目录中执行 jar cvf HelloService.aar . 进行打包。

Java WebService记

Java WebService记

将打包后的文件复制至 WEB-INF/services 目录下,即可在服务列表中看到新注册的服务,或者在 Axis 后台中也可以上传包部署(因此如果应用程序的Axis后台可访问且为默认凭据即可部署恶意Service获取权限)。

Java WebService记

Java WebService记

客户端服务调用

=======

调用 Web Service 可通过代码的方式实现也可以通过WSDL构造SOAP协议调用方法,最简便的方法则是使用SoapUI,其会根据 Web Service 的WSDL生成对应方法的SOAP协议请求。

// 代码实现Web Service调用

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

public class WebServiceClient {

public static void main(String[] args) throws Exception {

RPCServiceClient serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();

EndpointReference targetEPR = new EndpointReference(“http://192.168.0.105:8080/Axis2Demo_war_exploded/services/HelloService”);

options.setTo(targetEPR);

Object[] entryArgs = new Object[]{4, 2};

QName qName = new QName(“http://ws.apache.org/axis2”, “add”);

Object result = serviceClient.invokeBlocking(qName, entryArgs, new Class[]{int.class})[0];

qName = new QName(“http://ws.apache.org/axis2”, “send”);

serviceClient.invokeRobust(qName, new Object[]{“hello world!”});

}

}

Soap UI

Java WebService记

Java WebService记

Apache CXF

==========

Apache CXF是一个开源的、全功能的,容易使用的Web服务框架。CXF是两个项目的结合:由IONA技术公司开发的Celtix和由Codehaus主持的团队开发的XFire。

CXF支持的特性非常广泛,但特性主要在以下一些方面:

  • 支持的Web服务标准包括: SOAP WS-Addressing WS-Policy WS-ReliableMessaging WS-Security WS-SecurityPolicy WS-SecureConversation

  • JAS-WS API,用于Web服务开发 WSDL优先支持工具 Java优先支持

  • JAX-RS(JSR 311 1.0)API,用于RESTful Web服务开发

⬆️内容摘自Wiki百科。

发布服务

====

使用 Maven 构建项目,POM文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd”>

4.0.0

org.example

CXFDemo

1.0-SNAPSHOT

org.apache.cxf

cxf-rt-frontend-jaxws

3.4.0

org.apache.cxf

cxf-rt-transports-http

3.4.0

org.apache.cxf

cxf-rt-transports-http-jetty

3.4.0

org.apache.cxf

cxf-rt-transports-http-jetty

3.4.0

编写一个服务接口,定义 sayHi 方法:

package org.example.services;

import javax.jws.WebService;

// 声明这是一个Ws服务接口

@WebService

public interface HelloWorld {

// 定义服务方法

String sayHi(String name);

}

编写一个服务接口的实现类:

package org.example.services;

import javax.jws.WebService;

@WebService(endpointInterface = “org.example.services.HelloWorld”, serviceName = “HelloWorld”)

public class HelloWorldImpl implements HelloWorld {

public String sayHi(String name) {

return "hi, " + name;

}

}

再编写一个发布服务的主类 Main :

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import org.example.services.HelloWorld;

import org.example.services.HelloWorldImpl;

public class Main {

最后

总的来说,面试官要是考察思路就会从你实际做过的项目入手,考察你实际编码能力,就会让你在电脑敲代码,看你用什么编辑器、插件、编码习惯等。所以我们在回答面试官问题时,有一个清晰的逻辑思路,清楚知道自己在和面试官说项目说技术时的话就好了

pl implements HelloWorld {

public String sayHi(String name) {

return "hi, " + name;

}

}

再编写一个发布服务的主类 Main :

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import org.example.services.HelloWorld;

import org.example.services.HelloWorldImpl;

public class Main {

最后

总的来说,面试官要是考察思路就会从你实际做过的项目入手,考察你实际编码能力,就会让你在电脑敲代码,看你用什么编辑器、插件、编码习惯等。所以我们在回答面试官问题时,有一个清晰的逻辑思路,清楚知道自己在和面试官说项目说技术时的话就好了

[外链图片转存中…(img-YIdcWISY-1720090990881)]

[外链图片转存中…(img-ScyT4msQ-1720090990882)]

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值