基于JAX-WS实现RESTful形式的web服务端点(endpoint)

RESTful形式的web服务使用XML/HTTP绑定,实现jakarta.xml.ws.Provider。
服务端的实现类用jakarta.xml.ws.WebServiceProvider和BindingType(value=HTTPBinding.HTTP_BINDING)注释。其中HTTPBinding.HTTP_BINDING表示使用XML/HTTP绑定。

例如:

@WebServiceProvider
@BindingType(value=HTTPBinding.HTTP_BINDING)
public class Register implements Provider<Source>

服务端的实现类示例:

package com.thb.server;

import jakarta.annotation.Resource;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import jakarta.xml.ws.BindingType;
import jakarta.xml.ws.Provider;
import jakarta.xml.ws.WebServiceContext;
import jakarta.xml.ws.WebServiceProvider;
import jakarta.xml.ws.handler.MessageContext;
import jakarta.xml.ws.http.HTTPBinding;
import jakarta.xml.ws.http.HTTPException;
import java.io.ByteArrayInputStream;

@WebServiceProvider
@BindingType(value=HTTPBinding.HTTP_BINDING)
public class Register implements Provider<Source> {

    @Resource(type=Object.class)
    protected WebServiceContext wsContext;

    public Source invoke(Source source) {
        try {
            MessageContext mc = wsContext.getMessageContext();
            String query = (String)mc.get(MessageContext.QUERY_STRING);
            String path = (String)mc.get(MessageContext.PATH_INFO);
            System.out.println("Query String = "+query);
            System.out.println("PathInfo = "+path);
            return createSource(query);
        } catch(Exception e) {
            e.printStackTrace();
            throw new HTTPException(500);
        }
    }
    
    private Source createSource(String str) {
        String result = "1";
        String body =
            ""
            +result
            +"";
        Source source = new StreamSource(
            new ByteArrayInputStream(body.getBytes()));
        return source;
    }
    
}

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>restful</description>
    <display-name>restful</display-name>
    <listener>
        <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
    </listener>
    <servlet>
        <description>JAX-WS endpoint - restful</description>
        <display-name>restful</display-name>
        <servlet-name>restful</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>restful</servlet-name>
        <url-pattern>/register/*
    
    
        60
    

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="power-restful-register"
        implementation="com.thb.server.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.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

在浏览器中访问:
基于JAX-WS实现RESTful形式的web服务端点(endpoint)_第1张图片

你可能感兴趣的:(JAX-WS,web服务,RESTful)