无法从spring项目创建wsdl

时间:2014-10-30 18:13:12

标签: xml spring web-services spring-mvc soap

我正在尝试了解Spring网络服务,并且我遵循了this

我下载了项目并且我能够运行它。然后我创建了一个包含一些tweek的新Spring项目。 现在我可以从xsd文件中获取java类,但是无法创建最终的wsdl。

以下是我的档案:

AccountDetails.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://data.ws.akash.com" 
            targetNamespace="http://data.ws.akash.com" elementFormDefault="qualified" 
            attributeFormDefault="unqualified">

    <xs:element name="UserDetails" type="UserDetails"/>
    <xs:complexType name="UserDetails">
        <xs:sequence>
            <xs:element name="userId" type="xs:string"/>
            <xs:element name="username" type="xs:string"/>
            <xs:element name="age" type="xs:int"/>
            <xs:element name="emailId" type="xs:string"/>
            <xs:element name="userSalary" type="xs:double"/>
            <xs:element name="userGender" type="UserGender"/>
        </xs:sequence>
    </xs:complexType>
    <xs:simpleType name="UserGender">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Male"/>
            <xs:enumeration value="Female"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

AccountDetailsS​​erviceOperations.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns="http://reqres.data.ws.akash.com" 
        xmlns:userDetails="http://data.ws.akash.com" 
        targetNamespace="http://reqres.data.ws.akash.com" elementFormDefault="qualified">

    <xsd:import namespace="http://data.ws.akash.com" schemaLocation="AccountDetails.xsd"/>
    <xsd:element name="UserDetailsRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="userId" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="UserDetailsResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="AccountDetails" type="userDetails:UserDetails"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

UserDetailsS​​ervice.java

package com.akash.ws.service;

import com.akash.ws.data.reqres.UserDetailsResponse;

public interface UserDetailsService {

    public UserDetailsResponse getUserDetails(String userId);
}

UserDetailsS​​erviceImpl.java

package com.akash.ws.service.impl;

import org.springframework.stereotype.Service;

import com.akash.ws.data.UserDetails;
import com.akash.ws.data.UserGender;
import com.akash.ws.data.reqres.UserDetailsResponse;
import com.akash.ws.service.UserDetailsService;

@Service("userService")
public class UserDetailsServiceImpl implements UserDetailsService {

    public UserDetailsResponse getUserDetails(String userId) {
        UserDetailsResponse udr=new UserDetailsResponse();
        UserDetails ud=new UserDetails();
        ud.setAge(35);
        ud.setEmailId("obama@gmail.com");
        ud.setUserGender(UserGender.MALE);
        ud.setUserId("1");
        ud.setUsername("obama");
        ud.setUserSalary(200.20);
        udr.setAccountDetails(ud);
        return udr;
    }
}

UserDetailEndpoint.java

package com.akash.ws.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.akash.ws.data.reqres.UserDetailsRequest;
import com.akash.ws.data.reqres.UserDetailsResponse;
import com.akash.ws.service.UserDetailsService;

@Endpoint
public class UserDetailEndpoint {

    private static final String NAMESPACE="http://reqres.data.ws.akash.com";

    @Autowired
    @Qualifier("userService")
    private UserDetailsService userDetailsService;

    @PayloadRoot(localPart="getUserDetail",namespace=NAMESPACE)
    public @ResponsePayload UserDetailsResponse getUserDetail(@RequestPayload UserDetailsRequest userDetail) {

        return userDetailsService.getUserDetails(userDetail.getUserId());
    }
}

的web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         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">

    <display-name>MySpringWS</display-name>

   <!--
        - Location of the XML file that defines the root application context.
        - Applied by ContextLoaderListener.
    -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!--
        - Servlet that dispatches request to registered handlers (Controller implementations).
    -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet>  
            <servlet-name>webservices</servlet-name>  
            <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>  
            <init-param>  
                 <param-name>transformWsdlLocations</param-name>  
                 <param-value>true</param-value>  
            </init-param>  
            <init-param>  
                 <param-name>contextConfigLocation</param-name>  
                 <param-value></param-value>  
            </init-param>  
            <load-on-startup>1</load-on-startup>  
       </servlet>  
       <servlet-mapping>  
            <servlet-name>webservices</servlet-name>  
            <url-pattern>*.wsdl</url-pattern>  
       </servlet-mapping>  
       <servlet-mapping>  
            <servlet-name>webservices</servlet-name>  
           <url-pattern>/endpoints/*</url-pattern>  
       </servlet-mapping>

</web-app>

MVC-config.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:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:sws="http://www.springframework.org/schema/web-services"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/web-services  
            http://www.springframework.org/schema/web-services/web-services-2.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">



    <context:component-scan base-package="com.akash.ws.data com.akash.ws.data.reqres com.akash.ws.endpoint 
                                    com.akash.ws.service com.akash.ws.service.impl"/>

    <mvc:annotation-driven />
    <sws:annotation-driven />

    <bean id="UserDetailsService" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition" lazy-init="true">  
        <property name="schemaCollection">  
            <bean class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">  
                <property name="inline" value="true" />  
                <property name="xsds">  
                    <list>  
                        <value>schema/AccountDetailsServiceOperations.xsd</value>  
                    </list>  
                </property>  
            </bean>  
        </property>  
        <property name="portTypeName" value="UserDetailsService"/>  
        <property name="serviceName" value="UserDetailsServices" />  
        <property name="locationUri" value="/endpoints"/>  
    </bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
            <property name="prefix" value="/WEB-INF/view/"/>
            <property name="suffix" value=".jsp"/>
    </bean>

</beans>

的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples.service.service</groupId>
  <artifactId>MySpringWS</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>

    <properties>

        <!-- Generic properties -->
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>


        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>
        <spring.ws.version>2.0.0.RELEASE</spring.ws.version>

        <!-- Hibernate / JPA -->
        <hibernate.version>4.2.1.Final</hibernate.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>1.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
                <configuration>
                    <clearOutputDir>false</clearOutputDir>
                    <outputDirectory>src/main/java</outputDirectory>
                    <schemaDirectory>src/main/webapp/schema</schemaDirectory>
                    <includeSchema>**/*.xsd</includeSchema>
                    <bindingDirectory>src/main/resources/bindings</bindingDirectory>
                    <enableIntrospection>false</enableIntrospection>
                </configuration>
            </plugin>

            <plugin>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <version>9.2.3.v20140905</version>
            </plugin>

        </plugins>
    </build>

    <dependencies>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>${spring.ws.version}</version>
        </dependency>


        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>


        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies> 
</project>

以下是mvn命令:

mvn clean install
mvn jetty:run

请帮帮我。

1 个答案:

答案 0 :(得分:0)

使用dynamic-wsdl bean创建WSDL,例如 -

<sws:dynamic-wsdl id="orders"
  portTypeName="Orders"
  locationUri="http://localhost:8080/ordersService/">
  <sws:xsd location="/WEB-INF/xsd/Orders.xsd"/>
</sws:dynamic-wsdl>