java 编写jax-ws web service并生成没有XSD的WSDL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17399718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:53:50  来源:igfitidea点击:

Write jax-ws web service and generate WSDL without XSD

javaweb-servicestomcatwsdljax-ws

提问by mariami

I wrote a simple JAX-WS web service for tomcat application server on java.

我为 java 上的 tomcat 应用程序服务器编写了一个简单的 JAX-WS Web 服务。

I have one interface and on implementation class:
interface

我有一个接口和实现类:
接口

@WebService(name = "myWs")
@SOAPBinding(style = Style.RPC)
public interface IMyWs {
    @WebMethod(operationName = "getUser")
    Response getUser(@WebParam(name = "phone", mode = Mode.IN) String phone);

}


implementation


执行

@WebService(endpointInterface = "ge.mari.IMyWs")
public class MyWs implements IMyWs {
    @Override
    public Response getUser(String phone) {
               // SOME CODE
        return response;
    }
}

My problem is that, in my wsdl file Response class is defined in xsd file.
This is the snippet from my wsdl file

我的问题是,在我的 wsdl 文件中,响应类是在 xsd 文件中定义的。
这是我的 wsdl 文件的片段

<types>
<xsd:schema>
          <xsd:import namespace="http://ws.mari.ge/" schemaLocation="http://localhost:8080/MyServcie/MyWs?xsd=1">
</xsd:import>
</xsd:schema>
</types>

How can I make web service to generate all types in WSDL file instead of separate XSD file?
Should I change any configuration or add some annotation to my web service?

如何让 Web 服务在 WSDL 文件而不是单独的 XSD 文件中生成所有类型?
我应该更改任何配置或向我的 Web 服务添加一些注释吗?

回答by Kallja

You can have JAX-WS insert the generated schema into your WSDL file by using the

您可以使用 JAX-WS 将生成的模式插入到您的 WSDL 文件中

-inlineSchemas

command line switch. [1]

命令行开关。[1]

If you're using Maven in your project you can configure the JAX-WS maven plugin to do the same with the inlineSchemasconfiguration element in your execution configuration as follows: [2]

如果您在项目中使用 Maven,您可以配置 JAX-WS maven 插件以对执行配置中的inlineSchemas配置元素执行相同的操作,如下所示:[2]

<plugin>
  <groupId>org.jvnet.jax-ws-commons</groupId>
  <artifactId>jaxws-maven-plugin</artifactId>
  <version>2.2</version>
  <executions>
    <execution>
      <id>SomeId</id>
      <goals>
        <goal>wsgen</goal>
      </goals>
      <phase>prepare-package</phase>
      <configuration>
        <sei>some.class.Name</sei>
        <genWsdl>true</genWsdl>
        <keep>true</keep>
        <resourceDestDir>some/target/dir</resourceDestDir>
        <inlineSchemas>true</inlineSchemas>
      </configuration>
    </execution>
  </executions>
</plugin>

No changes to your Java class are necessary.

无需更改您的 Java 类。

[1] http://jax-ws.java.net/nonav/2.2.1/docs/wsgen.html

[1] http://jax-ws.java.net/nonav/2.2.1/docs/wsgen.html

[2] http://jax-ws-commons.java.net/jaxws-maven-plugin/wsgen-mojo.html

[2] http://jax-ws-commons.java.net/jaxws-maven-plugin/wsgen-mojo.html

回答by debuglevel

It is actually notpossible to use inlineSchemaswith the runtime WSDL generator. I debugged the WSDL generation and found this line in the EndpointFactory, where the inlineSchemasfeature (which actually ispresent in the wsgentool) is just set to false:

它实际上是能够使用inlineSchemas与运行WSDL生成。我调试的WSDL生成,发现这条线的EndpointFactory,那里的inlineSchemas功能(这实际上存在于wsgen工具)只是设置为false

    /**
     * Generates the WSDL and XML Schema for the endpoint if necessary
     * It generates WSDL only for SOAP1.1, and for XSOAP1.2 bindings
     */
    private static SDDocumentImpl generateWSDL(WSBinding binding, AbstractSEIModelImpl seiModel, Collection<SDDocumentImpl> docs,
                                               Container container, Class implType) {
        // [...]
        WSDLGenInfo wsdlGenInfo = new WSDLGenInfo(); 
        // [...]
        wsdlGenInfo.setInlineSchemas(false);
        // [...]
        seiModel.getDatabinding().generateWSDL(wsdlGenInfo);
        // [...]
    }

https://github.com/eclipse-ee4j/metro-jax-ws/blob/f37dae6bdfd03bafdad63ed05b27dbfc3c38af1b/jaxws-ri/rt/src/main/java/com/sun/xml/ws/server/EndpointFactory.java#L658

https://github.com/eclipse-ee4j/metro-jax-ws/blob/f37dae6bdfd03bafdad63ed05b27dbfc3c38af1b/jaxws-ri/rt/src/main/java/com/sun/xml/ws/server/EndpointFactory.java#L658

There is also an open issue for JAX-WS to change this (but I guess there is not much hope for changes in JAX-WS anymore). https://github.com/eclipse-ee4j/metro-jax-ws/issues/49

JAX-WS 也有一个未解决的问题来改变这一点(但我想 JAX-WS 中的改变不再有太大希望)。 https://github.com/eclipse-ee4j/metro-jax-ws/issues/49

回答by secra

AFAIK it ist not possible to have JAX generate a WSDL with schemas inline.

AFAIK 不可能让 JAX 生成具有内联模式的 WSDL。

BTW: Separating the WSDL definition and the XSD schema is a good move (you might want to use the object structure defined by the schema in a different context e.g. storing data to files or something like that).

顺便说一句:将 WSDL 定义和 XSD 模式分开是一个很好的举措(您可能希望在不同的上下文中使用模式定义的对象结构,例如将数据存储到文件或类似的东西)。

That said: If you needan "all in one" WSDL (because some ancient client requires it) you can always have jax-wsgenerate the WSDL initially and then edit it to your heart's content. The edited WSDL can be included using the wsdlLocationparameter of the @WebServiceannotation.

也就是说:如果您需要一个“一体式”WSDL(因为某些古老的客户端需要它),您始终可以首先jax-ws生成 WSDL,然后根据您的喜好对其进行编辑。可以使用注释的wsdlLocation参数包含已编辑的 WSDL @WebService