RESTful web service based on JAX-WS returns xml document generated through JAXB annotations

A RESTful web service written based on JAX-WS that returns xml documents. This xml document can be generated based on JAXB annotations, simplifying xml generation.

In order to use the dependent libraries, you can add the following dependencies in the pom.xml file of the maven project:

<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>

For example, the RegisterResponse class below uses JAXB annotations:

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;
    }
}

Corresponding to the form of the generated xml document:

<response>
<flag>1</flag>
<body>20012</body>
</response>

The following is the complete sample code on the server side:

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;
    }
}

The web.xml file of the deployed web service:

<?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 file:

<?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>

The directory layout of the web application deployed to the Tomcat container:

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

Access the web service in the browser: