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



### 测试WebService

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

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

标签: webservicejava
分享给朋友:

相关文章

java如何下载

java如何下载

下载Java的步骤 访问Oracle官方网站或OpenJDK项目页面下载Java开发工具包(JDK)。Oracle JDK适用于商业用途,OpenJDK为开源版本。 打开浏览器,进入Oracle J…

java如何编程

java如何编程

Java编程基础 Java是一种面向对象的编程语言,广泛应用于企业级开发、移动应用(Android)等领域。以下是Java编程的核心步骤和示例。 环境搭建 安装JDK 从Oracle官网下载适合…

如何运行java程序

如何运行java程序

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

如何编写java程序

如何编写java程序

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

java如何输出数组

java如何输出数组

输出数组的方法 在Java中,输出数组内容有多种方式,具体取决于数组类型和输出格式需求。以下是几种常见的方法: 使用Arrays.toString()方法 适用于一维数组,直接调用Arrays.to…

如何运行java项目

如何运行java项目

运行Java项目的步骤 确保Java环境已安装 在运行Java项目前,需确认系统已安装Java Development Kit(JDK)。通过命令行输入java -version和javac -ver…