当前位置:首页 > 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测试

如何发布webservice java

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

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

标签: webservicejava
分享给朋友:

相关文章

如何打开java

如何打开java

打开 Java 程序的方法 通过命令行运行 Java 程序 确保已安装 Java Development Kit (JDK) 并配置环境变量。使用 javac 编译 .java 文件,生成 .clas…

如何运行java程序

如何运行java程序

编写Java代码 创建一个以.java为扩展名的文件,例如HelloWorld.java。文件内容需包含一个类定义,类名必须与文件名一致。例如: public class HelloWorld {…

java如何学习

java如何学习

学习Java的有效方法 理解基础概念 Java是一门面向对象的编程语言,掌握基础概念如变量、数据类型、运算符、控制语句(if-else、循环)是必要的。面向对象的核心概念包括类、对象、继承、多态和封装…

java如何删除文件

java如何删除文件

删除文件的基本方法 使用 java.io.File 类的 delete() 方法可以删除文件。如果文件存在且删除成功,返回 true;否则返回 false。 import java.io.Fil…

java前景如何

java前景如何

Java 当前的市场需求 Java 在企业级开发、大数据、云计算等领域仍占据重要地位。许多大型企业(如银行、电商平台)依赖 Java 构建后端系统。招聘平台数据显示,Java 开发岗位数量稳定,尤其在…

java如何生成随机数

java如何生成随机数

生成随机数的方法 在Java中生成随机数可以通过多种方式实现,以下是几种常见的方法: 使用Math.random()方法 Math.random()方法返回一个double类型的伪随机数,范围在[0…