基于JAX-WS编写的RESTful web服务,返回xml文档。这个xml文档可以基于JAXB注解的形式来生成,简化xml的生成。
例如,下面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"?>
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="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
│
└─lib
angus-activation-1.0.0.jar
angus-mail-1.0.0.jar
FastInfoset-2.1.0.jar
gmbal-api-only-4.0.3.jar
ha-api-3.1.13.jar
jakarta.activation-api-2.1.0.jar
jakarta.annotation-api-2.1.1.jar
jakarta.mail-api-2.1.0.jar
jakarta.xml.bind-api-4.0.0.jar
jakarta.xml.soap-api-3.0.0.jar
jakarta.xml.ws-api-4.0.0.jar
jaxb-core-4.0.0.jar
jaxb-impl-4.0.0.jar
jaxws-rt-4.0.0.jar
log4j-api-2.20.0.jar
log4j-core-2.20.0.jar
management-api-3.2.3.jar
mimepull-1.10.0.jar
saaj-impl-3.0.0.jar
stax-ex-2.1.0.jar
stax2-api-4.2.1.jar
streambuffer-2.1.0.jar
woodstox-core-6.2.8.jar
在浏览器中访问该web服务: