当前位置:首页 > Java

如何发布webservice java

2026-03-03 13:08:18Java

使用JAX-WS发布WebService

在Java中发布WebService可以通过JAX-WS(Java API for XML Web Services)实现。JAX-WS是Java EE的一部分,简化了WebService的创建和部署。

  1. 创建WebService接口 定义一个带有@WebService注解的接口或类:
    
    import javax.jws.WebService;
    import javax.jws.WebMethod;

@WebService public interface Calculator { @WebMethod int add(int a, int b); }


2. 实现WebService接口
```java
@WebService(endpointInterface = "com.example.Calculator")
public class CalculatorImpl implements Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}
  1. 发布WebService 使用Endpoint类发布服务:
    
    import javax.xml.ws.Endpoint;

public class Publisher { public static void main(String[] args) { Endpoint.publish("http://localhost:8080/calculator", new CalculatorImpl()); System.out.println("Service published at http://localhost:8080/calculator"); } }


### 使用Spring Boot发布WebService

对于现代Java应用,Spring Boot提供了更便捷的WebService发布方式。

1. 添加依赖
在`pom.xml`中添加:
```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
  1. 创建WebService端点
    
    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 CalculatorEndpoint { private static final String NAMESPACE_URI = "http://example.com/calculator";

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "AddRequest")
@ResponsePayload
public AddResponse add(@RequestPayload AddRequest request) {
    AddResponse response = new AddResponse();
    response.setResult(request.getA() + request.getB());
    return response;
}

}


3. 配置WebService
```java
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.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

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

    @Bean(name = "calculator")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema calculatorSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CalculatorPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://example.com/calculator");
        wsdl11Definition.setSchema(calculatorSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema calculatorSchema() {
        return new SimpleXsdSchema(new ClassPathResource("calculator.xsd"));
    }
}

使用Apache CXF发布WebService

Apache CXF是另一个流行的WebService框架,提供了更多高级功能。

  1. 添加依赖

    <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-frontend-jaxws</artifactId>
     <version>3.4.0</version>
    </dependency>
    <dependency>
     <groupId>org.apache.cxf</groupId>
     <artifactId>cxf-rt-transports-http</artifactId>
     <version>3.4.0</version>
    </dependency>
  2. 配置和发布服务

    
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class CxfPublisher { public static void main(String[] args) { JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); factory.setServiceClass(CalculatorImpl.class); factory.setAddress("http://localhost:8080/calculator"); factory.create(); System.out.println("CXF Service published at http://localhost:8080/calculator"); } }



### 测试WebService

可以使用SOAPUI或Postman等工具测试发布的WebService。对于JAX-WS发布的简单服务,可以通过浏览器访问`http://localhost:8080/calculator?wsdl`查看WSDL定义。

对于Spring Boot应用,WSDL通常位于`http://localhost:8080/ws/calculator.wsdl`。CXF发布的WSDL也可以通过类似URL访问。

如何发布webservice java

标签: webservicejava
分享给朋友:

相关文章

java如何学习

java如何学习

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

java如何创建对象

java如何创建对象

创建对象的基本方法 在Java中,创建对象主要通过new关键字调用构造函数完成。基本语法为: ClassName objectName = new ClassName(); 例如创建String对…

如何配置java环境变量

如何配置java环境变量

下载并安装JDK 从Oracle官网或OpenJDK项目下载适合操作系统的JDK安装包。运行安装程序,按照提示完成安装,默认路径通常为C:\Program Files\Java\jdk-版本号。…

如何系统的学习java

如何系统的学习java

学习Java的基础知识 Java的基础知识包括语法、数据类型、变量、运算符、控制流语句等。可以通过官方文档或入门书籍如《Java核心技术》来掌握这些内容。编写简单的程序练习基础语法,例如打印“Hell…

如何学习好java

如何学习好java

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

vue实现java登录注册

vue实现java登录注册

实现 Vue 与 Java 后端的登录注册功能 前端 Vue 部分 安装必要的依赖(如 axios 用于 HTTP 请求): npm install axios 创建登录表单组件(Login.vu…