jaxws-spring 搭建Web Services笔记

虽知道webservice,但一直没使用过,但最近公司涉及到这方面的需求,就简单的研究了一下。顺便做了个笔记,就在这里跟大家分享下,学习用。大神请忽略,如有错误之处,敬请指点。

WebService顾名思义就是web服务,web服务主要有两种,一种是基于soap类型的服务,一种是基于rest类型的服务,其中soap类型的服务有两种版本,一种是soap1.1版本,一种是soap1.2版本,soap服务类型的数据是xml数据格式的,而rest服务的数据类型是json格式的,这里主要用到dsoap类型的服务。

wsdl(网络服务描述语言)是Web Service的描述语言,也就是说wsdl文件是soap的使用说明书。认识WSDL是非常有必要的,只有能看懂WSDL文件,我们才可以去调用soap类型的Web服务,如果不了解可以搜索一下。

java开发webservice的几种方式

Axis2,Apche CXF,jax-ws其他的还不了解,这次主要用的还是jax-ws,下面就进入笔记流程。

1.maven相关pom.xml

<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>net.transino.lms</groupId>
    <artifactId>ws</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>ws Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.8.RELEASE</version>
        </dependency>

        <!-- jax与 spring 集成 -->
        <dependency>
            <groupId>org.jvnet.jax-ws-commons.spring</groupId>
            <artifactId>jaxws-spring</artifactId>
            <version>1.8</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>
    </dependencies>
    <build>
        <finalName>ws</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

istack-commons-runtime包的作用是,解决 noclass XMLStreamReaderToContentHandler问题(添加jaxws-rt也可以解决noclass问题,但会出现xsd文件异常)

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <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>

    <!-- Start WebService Config -->
    <servlet>
        <servlet-name>JAXWSServlet</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>JAXWSServlet</servlet-name>
        <url-pattern>/service/hi</url-pattern>
    </servlet-mapping>

</web-app>

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

    <context:annotation-config/>
    <context:component-scan base-package="net.transino.lms"/>

    <wss:binding url="/service/hi">
        <wss:service>
            <ws:service bean="#helloService" />
        </wss:service>
    </wss:binding>
</beans>

java 代码 interface HelloService

package net.transino.lms.ws;

import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService(targetNamespace ="ws.lms.transino.net", serviceName = "helloService")
@SOAPBinding(style = Style.RPC)
public interface HelloService {

    @WebResult(name="group")
    Group sayHi(@WebParam(name="user") User u);
}

class HelloServiceImpl

package net.transino.lms.ws;

import org.springframework.stereotype.Component;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@Component("helloService")
@WebService(endpointInterface = "net.transino.lms.ws.HelloService",
        targetNamespace ="ws.lms.transino.net", serviceName = "helloService")
@SOAPBinding(style = Style.RPC)
public class HelloServiceImpl implements HelloService {

    @Override
    public Group sayHi(User u) {
        return new Group(u);
    }
}

还有两Bean的代码比较简单就不贴了,最后是运行结果。


还有生成的WSDL


服务端还没遇到太多坑,网上查查就好了。但如果要整合到现有项目里可能会遇到jar冲突的问题,下篇写个客户端访问的工程。


猜你喜欢

转载自blog.csdn.net/lkw5657/article/details/80284664
今日推荐