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

如何发布webservice java

<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描述的情况:

如何发布webservice java

使用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地址进行测试。

标签: webservicejava
分享给朋友:

相关文章

java如何导包

java如何导包

导入包的方法 在Java中,导入包(package)可以通过import语句实现,用于引入其他类或包中的功能。以下是几种常见的导包方式: 导入单个类 import java.util.ArrayL…

java如何处理高并发

java如何处理高并发

Java处理高并发的方法 使用多线程和线程池 Java的java.util.concurrent包提供了线程池(如ThreadPoolExecutor)和并发工具类(如CountDownLatch、C…

如何学习好java

如何学习好java

掌握Java基础知识 学习Java的第一步是掌握其基础知识,包括数据类型、变量、运算符、控制流语句(如if-else、for循环、while循环)以及数组。这些是构建更复杂程序的基石。可以通过在线教程…

java实现vue接口

java实现vue接口

Java 实现 Vue 接口的方法 在 Java 中实现 Vue 前端调用的接口,通常需要使用 Spring Boot 框架构建 RESTful API。以下是具体的实现步骤和代码示例。 创建 Sp…

php 实现 java md5

php 实现 java md5

在 PHP 中实现与 Java 相同的 MD5 哈希算法,关键在于确保编码和输出格式的一致性。以下是具体实现方法和注意事项: 确保字符串编码一致 Java 的 String.getBytes() 默…

java js实现转盘抽奖

java js实现转盘抽奖

实现转盘抽奖的步骤 HTML结构 创建转盘抽奖的基本HTML结构,包括转盘区域和抽奖按钮。 <div id="wheel"> <canvas id="wheelCanv…