Java 用SOAP复制RESTAPI
我有一个简单的REST api,它以JSON格式发送数据,如下所示:Java 用SOAP复制RESTAPI,java,spring,grails,spring-ws,Java,Spring,Grails,Spring Ws,我有一个简单的REST api,它以JSON格式发送数据,如下所示: http://myapp/color (POST w/ data in JSON) - Creates a new color in DB http://myapp/color/id (GET) - Fetches details for a color from DB http://myapp/color (GET) - Fetches details for all colors in DB 我还想为这三个函数创建一个S
http://myapp/color (POST w/ data in JSON) - Creates a new color in DB
http://myapp/color/id (GET) - Fetches details for a color from DB
http://myapp/color (GET) - Fetches details for all colors in DB
我还想为这三个函数创建一个SOAP API。因此,我将使用Spring WS
我已经为创建创建创建了SOAP API。使用以下XSD
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:hr="http://www.myveryimportantcompany.com/tr/schemas"
targetNamespace="http://www.myveryimportantcompany.com/tr/schemas"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="ColorRequest">
<xs:complexType>
<xs:all>
<xs:element name="Color" type="hr:ColorType"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:complexType name="ColorType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="ColorResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="status" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我还为上述功能编写了功能测试,效果很好
问题-我过去从未创建过SOAP WS,因此如果问题愚蠢,请忽略
- 剩下的两个函数的XSD是什么样子的
- 每个函数是否需要单独的XSD
- 我可以用GUI测试SOAP服务吗?我给了soapgui一个演示,但它需要一个WSDL。我怎样才能创造呢李>
PS:我正在使用来自grails的SpringWS插件 我打赌这是一种主观问题。我将尽可能详细地回答,使其有用。请容忍我。:) 第一件事优先:-
- 插件使用契约优先的Web服务。您在这里创建的模式应该是底层Web服务的契约
- 每个Web服务都应该有自己的契约,这意味着它自己的模式
- SpringWS遵循一种惯例,即创建一个与合同
名称相同的模式
端点
- 您可以在端点中使用继承,这将帮助您为所有契约创建单个xsd模式。在这种情况下,您必须在相应的子端点中继承父端点
为了方便大家,让我们使用
用户
服务,而不是颜色
//Domain:-
class User{
String name
String email
Integer age
}
我将为这个实体准备一个基本模式,一些类似于您之前为Color
所做的:
<!-- User.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:hr="http://www.myveryimportantcompany.com/tr/schemas"
targetNamespace="http://www.myveryimportantcompany.com/tr/schemas"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:complexType name="UserType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
<xs:element name="User" type="hr:UserType"/>
</xs:schema>
现在来看每项服务的每份合同:-
<!-- GetUserService.xsd -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:hr="http://www.myveryimportantcompany.com/tr/schemas"
targetNamespace="http://www.myveryimportantcompany.com/tr/schemas"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="GetUserRequest" type="hr:GetUserRequestType"/>
<xs:complexType name="GetUserRequestType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="GetUserResponse" type="hr:GetUserResponseType"/>
<xs:complexType name="GetUserRequestType">
<xs:sequence>
<xs:element ref="hr:User"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
类似地,您可以让UpdateUserService.xsd
执行更新[POST,PUT]部分
或者,您可以将所有xsd累积到一个名为UserServices.xsd
警告如果所有服务只有一个xsd,那么您必须创建一个名为
UserServicesEndpoint.groovy
(请参阅springws文档如何创建端点)的父端点(完全相同),那么您的服务端点将与
类GetUserServiceEndpoint扩展UserServicesEndpoint{..}
class UpdateUserServiceEndpoint扩展UserServicesEndpoint{..}
如果所有XSD都是单独的,则不需要继承
问题的答案:
现在你知道它应该是什么样子了。名称应该类似于
UpdateColorService.xsd
,GetColorService.xsd
,GetAllColorsService.xsd
按照上面的。您可以使用单独的xsd,也可以将它们放在一个父xsd中。在这种情况下,还必须有父端点
是的,你可以。我找到的最好的工具是(免费版本对我来说就足够了)。你也可以使用,如果你觉得舒服的话。契约优先服务总是需要一个WSDL来运行。作为SpringWS插件的一部分,您拥有出色的DSL支持,可以在运行应用程序后为您创建WSDL。再次按照文档进行操作。我可以用另一个问题来解释这一点,因为这个答案现在已经成为一部史诗
- 最后,当整个世界都在安宁中时,为什么您需要SOAP服务呢
- springws插件几乎已经过时,多年来一直不受支持。我已经根据自己的需要定制了插件,但还没有将其提交给Grails插件。感觉不到吗?但我想我现在必须这么做,因为@Anthony正在使用它
- 插件在项目的
目录中有JAR,这可能与依赖项冲突lib
- 该插件使用了旧版本的
,它肯定会与springws-core
附带的grails2.2.x
崩溃springcore:3.1.2
- 较新版本的JDK(7或更高版本)可能会出现编译错误