当前位置:首页 > Java

如何发布webservice java

2026-02-05 01:55:26Java

使用JAX-WS发布WebService

在Java中发布WebService可以使用JAX-WS(Java API for XML Web Services)技术。以下是具体实现步骤:

创建服务端点接口(SEI)

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHello(String name);
}

实现服务端点接口

import javax.jws.WebService;

@WebService(endpointInterface = "com.example.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}

发布WebService

import javax.xml.ws.Endpoint;

public class WebServicePublisher {
    public static void main(String[] args) {
        String url = "http://localhost:8080/hello";
        Endpoint.publish(url, new HelloWorldImpl());
        System.out.println("WebService published at: " + url);
    }
}

使用Spring Boot发布WebService

对于现代Java应用,使用Spring Boot发布WebService更加简便:

添加依赖到pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

创建服务类

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class CountryEndpoint {
    private static final String NAMESPACE_URI = "http://example.com/webservice";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();
        // 实现业务逻辑
        return response;
    }
}

配置WebService

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.transport.http.MessageDispatcherServlet;

@EnableWs
@Configuration
public class WebServiceConfig {
    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext context) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(context);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*");
    }
}

生成WSDL文件

对于需要提供WSDL描述的情况:

使用JAX-WS自动生成

Endpoint.publish("http://localhost:8080/hello?wsdl", new HelloWorldImpl());

使用Spring WS生成

<bean id="countries" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition">
    <property name="schema" ref="schema"/>
    <property name="portTypeName" value="Countries"/>
    <property name="locationUri" value="/ws"/>
    <property name="targetNamespace" value="http://example.com/webservice"/>
</bean>

测试WebService

发布后可以通过SOAP客户端测试服务:

使用cURL测试

curl --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws

使用SOAPUI工具创建新项目并输入WSDL地址进行测试。

如何发布webservice java

标签: webservicejava
分享给朋友:

相关文章

如何运行java

如何运行java

运行Java程序的方法 安装Java开发工具包(JDK) 确保系统已安装JDK。可通过命令行输入 java -version 检查是否安装。若未安装,需从Oracle官网或OpenJDK下载并配置环境…

java如何react

java如何react

在Java中使用React 要在Java项目中集成React,通常需要将React前端与Java后端结合使用。以下是几种常见的方法: 使用Spring Boot作为后端 Spring Boot是一个…

如何卸载java

如何卸载java

卸载Java的步骤 Windows系统卸载方法 打开控制面板,选择“程序和功能”或“卸载程序”。在列表中找到Java相关条目(如“Java 8 Update 251”),右键选择卸载并跟随向导完成…

如何编写java程序

如何编写java程序

安装开发环境 下载并安装JDK(Java Development Kit),推荐从Oracle官网或OpenJDK获取最新版本。安装完成后配置环境变量,确保JAVA_HOME和PATH正确设置。…

java如何创建数组

java如何创建数组

创建数组的基本方法 在Java中,数组是固定大小的同类型元素集合。创建数组需要声明数组类型并初始化。 声明数组 语法格式为 数据类型[] 数组名 或 数据类型 数组名[]: int[] arr1;…

java如何创建项目

java如何创建项目

使用IDE创建Java项目(以IntelliJ IDEA为例) 打开IntelliJ IDEA,选择“New Project”。 在左侧菜单中选择“Java”,确保已配置JDK(若无需手动添加)。 勾…