基于JAX-WS编写的RESTful web服务,返回xml文档。这个xml文档可以基于JAXB注解的形式来生成,简化xml的生成。
为了使用依赖的库,可以在maven工程的pom.xml文件中增加如下依赖:
<dependency><groupId>jakarta.xml.ws</groupId><artifactId>jakarta.xml.ws-api</artifactId><version>4.0.0</version>
</dependency>
<dependency><groupId>com.sun.xml.ws</groupId><artifactId>jaxws-rt</artifactId><version>4.0.0</version>
</dependency>
例如,下面RegisterResponse 这个类使用了JAXB的注解:
package com.thb.server.register;import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "response")
public class RegisterResponse {@XmlElement(name = "flag")private int functionCode;@XmlElement(name = "body")private String enterpriseId;public RegisterResponse() {}public RegisterResponse(int functionCode, String enterpriseId) {this.functionCode = functionCode;this.enterpriseId = enterpriseId;}
}
对应生成的xml文档的形式:
<response><flag>1</flag><body>20012</body>
</response>
下面是服务端完整的示例代码:
package com.thb.server.register;import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.ws.BindingType;
import jakarta.xml.ws.Provider;
import jakarta.xml.ws.WebServiceProvider;
import jakarta.xml.ws.http.HTTPBinding;
import jakarta.xml.ws.http.HTTPException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;@WebServiceProvider
@BindingType(value=HTTPBinding.HTTP_BINDING)
public class Register implements Provider<Source> {public Source invoke(Source source) {try {return createSource();} catch(Exception e) {e.printStackTrace();throw new HTTPException(500);}}private Source createSource() throws JAXBException {JAXBContext context = JAXBContext.newInstance(RegisterResponse.class);Marshaller marshaller = context.createMarshaller();ByteArrayOutputStream outputStream = new ByteArrayOutputStream();marshaller.marshal(new RegisterResponse(1, "20012"), outputStream);byte[] bytes = outputStream.toByteArray();ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);Source source = new StreamSource(inputStream);return source;}}
package com.thb.server.register;import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "response")
public class RegisterResponse {@XmlElement(name = "flag")private int functionCode;@XmlElement(name = "body")private String enterpriseId;public RegisterResponse() {}public RegisterResponse(int functionCode, String enterpriseId) {this.functionCode = functionCode;this.enterpriseId = enterpriseId;}
}
部署的web服务的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?><web-app version="6.0" xmlns="https://jakarta.ee/xml/ns/jakartaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"><description>power-restful</description><display-name>power-restful</display-name><listener><listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class></listener><servlet><description>The JAX-WS dispatcher servlet</description><display-name>dispatcher</display-name><servlet-name>dispatcher</servlet-name><servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/register/*</url-pattern></servlet-mapping><session-config><session-timeout>60</session-timeout></session-config>
</web-app>
sun-jaxws.xml文件:
<?xml version="1.0" encoding="UTF-8"?><endpointsxmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"version="2.0"><endpointname="restful-register"implementation="com.thb.server.register.Register"url-pattern="/register/*" />
</endpoints>
部署到Tomcat容器中该web应用的目录布局:
D:\APACHE-TOMCAT-10.1.13\WEBAPPS\POWER-RESTFUL
├─META-INF
│ MANIFEST.MF
│ war-tracker
│
└─WEB-INF│ sun-jaxws.xml│ web.xml│ ├─classes│ └─com│ └─thb│ └─server│ └─register│ Register.class│ RegisterResponse.class│ └─libangus-activation-1.0.0.jarangus-mail-1.0.0.jarFastInfoset-2.1.0.jargmbal-api-only-4.0.3.jarha-api-3.1.13.jarjakarta.activation-api-2.1.0.jarjakarta.annotation-api-2.1.1.jarjakarta.mail-api-2.1.0.jarjakarta.xml.bind-api-4.0.0.jarjakarta.xml.soap-api-3.0.0.jarjakarta.xml.ws-api-4.0.0.jarjaxb-core-4.0.0.jarjaxb-impl-4.0.0.jarjaxws-rt-4.0.0.jarlog4j-api-2.20.0.jarlog4j-core-2.20.0.jarmanagement-api-3.2.3.jarmimepull-1.10.0.jarsaaj-impl-3.0.0.jarstax-ex-2.1.0.jarstax2-api-4.2.1.jarstreambuffer-2.1.0.jarwoodstox-core-6.2.8.jar
在浏览器中访问该web服务: