WebService在Spring环境下配置

项目使用maven构建,开发工具IntelliJ IDEA

WebService配置:

pom.xml中加入:

<!-- jax-ws -->
<dependency>
    <groupId>org.jvnet.jax-ws-commons.spring</groupId>
    <artifactId>jaxws-spring</artifactId>
    <version>1.9</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.sun.istack</groupId>
    <artifactId>istack-commons-runtime</artifactId>
    <version>2.2.1</version>
</dependency>

web.xml中加入:

    <servlet>
        <servlet-name>springWsServlet</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>springWsServlet</servlet-name>
        <!--此处为webservice服务 匹配路径-->
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

在项目主目录下增加webservice文件夹存放你编写的webservice接口

新增一个webservice接口如下:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface WebServer {
    @WebMethod(operationName = "saySomething") //表明这是一个发布出webservice方法,方法名为设置的operationName
    String saySomething(@WebParam(name = "name") String name);
}

下面编写接口的实现类

@WebService(endpointInterface = "org.ihsusta.server.webservice.WebServer", // webservice接口类 名称
        serviceName = "webServerService", // 自定义 serviceName
        portName = "webServerPort", // 自定义 portName
        targetNamespace = "http://webservice.server.ihsusta.org/") // 自定义 namespace
@Component("webServerImpl") // 增加注解 注入spring容器中
public class WebServerImpl implements WebServer {
    public String saySomething(String name) {
        return "Hello " + name + "!";
    }
}

增加spring 配置文件 spring-webservice.xml,

<?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:wss="http://jax-ws.dev.java.net/spring/servlet"
       xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		    http://jax-ws.dev.java.net/spring/servlet
		     http://jax-ws.java.net/spring/servlet.xsd
		      http://jax-ws.dev.java.net/spring/core
		       http://jax-ws.dev.java.net/spring/core.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 注解扫描 -->
    <context:component-scan base-package="org.ihsusta.server.webservice"/>
    <!-- 发布出的webserice路径 -->
    <wss:binding url="/services/WebServer">
        <wss:service>
            <!-- 与webservice实现类的bean name 要相同-->
            <ws:service bean="#webServerImpl"/>
        </wss:service>
    </wss:binding>
</beans>

配置到此基本完成

启动项目后

浏览器输入:http://localhost:8080/webserver/services/WebServer 即可看到你发布出的wsdl

接下来 试试再webservice中注入spring容器中的其他bean

编写一个service接口及其实现类

public interface IndexService {

    void hello(String name);
}
@Service
public class IndexServiceImpl implements IndexService {

    public void hello(String name) {
        System.out.println("I am IndexService, hello " + name + " !");
    }
}

再将之前编写WebServer进行修改

@WebService(endpointInterface = "org.ihsusta.server.webservice.WebServer", // webservice接口类 名称
        serviceName = "webServerService", // 自定义 serviceName
        portName = "webServerPort", // 自定义 portName
        targetNamespace = "http://webservice.server.ihsusta.org/") // 自定义 namespace
@Component("webServerImpl") // 增加注解 注入spring容器中
public class WebServerImpl implements WebServer {

    @Resource
    private IndexService service;  // add service

    public String saySomething(String name) {
        service.hello(name); // call service hello method
        return "Hello " + name + "!";
    }
}

对该webservice进行调用,控制台出现


说明service 已成功注入WebServer中

源码地址:https://github.com/atsushinee/webserver

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以按照以下步骤使用@WebService开发接口并可供访问: 1. 添加依赖:在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 2. 创建接口:创建一个接口并添加@WebService注解。例如: ```java @WebService public interface HelloWorld { String sayHello(String name); } ``` 3. 实现接口:创建一个实现类并实现接口中的方法。例如: ```java @Service public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 4. 配置WebService:在application.properties文件中添加以下配置: ```properties # 指定WebService接口所在的包路径 spring.webservices.type-mapping-package=com.example.webservice # 指定WebService服务的名称和命名空间 spring.webservices.namespace=http://example.com/webservice spring.webservices.servlet.init.wsdl-namespace=http://example.com/webservice spring.webservices.servlet.init.wsdl-location=classpath:/wsdl/helloworld.wsdl ``` 5. 创建WSDL文件:在resources目录下创建wsdl文件夹,并在其中创建helloworld.wsdl文件。例如: ```xml <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com/webservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorld" targetNamespace="http://example.com/webservice"> <wsdl:types> <xsd:schema targetNamespace="http://example.com/webservice"> <xsd:element name="sayHello"> <xsd:complexType> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="sayHelloResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="return" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="sayHelloRequest"> <wsdl:part element="tns:sayHello" name="parameters"/> </wsdl:message> <wsdl:message name="sayHelloResponse"> <wsdl:part element="tns:sayHelloResponse" name="parameters"/> </wsdl:message> <wsdl:portType name="HelloWorld"> <wsdl:operation name="sayHello"> <wsdl:input message="tns:sayHelloRequest"/> <wsdl:output message="tns:sayHelloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldSoap11" type="tns:HelloWorld"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHello"> <soap:operation soapAction=""/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorld"> <wsdl:port binding="tns:HelloWorldSoap11" name="HelloWorldSoap11"> <soap:address location="http://localhost:8080/ws"/> </wsdl:port> </wsdl:service> </wsdl:definitions> ``` 6. 启动应用程序:运行Spring Boot应用程序并访问http://localhost:8080/ws/helloworld.wsdl以获取WSDL信息。然后可以使用SOAP客户端或浏览器访问WebService接口。例如: ```xml <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://example.com/webservice"> <soapenv:Header/> <soapenv:Body> <web:sayHello> <name>World</name> </web:sayHello> </soapenv:Body> </soapenv:Envelope> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值