当前位置:首页 > Java

java如何写webservice接口

2026-03-17 18:30:36Java

使用 JAX-WS 创建 WebService 接口

JAX-WS 是 Java 提供的一个用于构建 SOAP 风格 WebService 的标准 API。

定义服务接口
创建一个 Java 接口,并使用 @WebService 注解标记:

import javax.jws.WebService;

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

实现服务接口
编写实现类,同样使用 @WebService 注解:

import javax.jws.WebService;

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

发布 WebService
使用 Endpoint 类发布服务:

import javax.xml.ws.Endpoint;

public class WebServicePublisher {
    public static void main(String[] args) {
        String address = "http://localhost:8080/helloWorld";
        Endpoint.publish(address, new HelloWorldImpl());
        System.out.println("WebService 已启动: " + address);
    }
}

使用 Spring Boot 创建 RESTful WebService

Spring Boot 提供了简单的方式创建 RESTful 风格的 WebService。

添加依赖
pom.xml 中添加 Spring Web 依赖:

java如何写webservice接口

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

定义 REST 控制器
使用 @RestController@RequestMapping 注解:

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class HelloController {
    @GetMapping("/hello")
    public String sayHello(@RequestParam String name) {
        return "Hello, " + name + "!";
    }
}

启动 Spring Boot 应用

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebServiceApplication.class, args);
    }
}

使用 Apache CXF 创建 WebService

Apache CXF 是一个支持 SOAP 和 REST 的 WebService 框架。

java如何写webservice接口

添加 CXF 依赖
pom.xml 中添加 CXF 相关依赖:

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.5.5</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.5.5</version>
</dependency>

配置 CXF Servlet
application.properties 中配置 CXF 路径:

cxf.path=/services

定义服务接口和实现

import javax.jws.WebService;

@WebService
public interface GreetingService {
    String greet(String name);
}

@WebService(endpointInterface = "com.example.GreetingService")
public class GreetingServiceImpl implements GreetingService {
    @Override
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}

发布 CXF WebService
在 Spring Boot 主类中配置 CXF:

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {
    @Autowired
    private Bus bus;

    @Bean
    public Endpoint greetingEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, new GreetingServiceImpl());
        endpoint.publish("/greeting");
        return endpoint;
    }
}

总结

  • JAX-WS 适用于 SOAP 风格的 WebService,适合企业级集成。
  • Spring Boot REST 适合轻量级的 RESTful 服务,开发效率高。
  • Apache CXF 提供更灵活的配置,支持 SOAP 和 REST 两种风格。

根据项目需求选择合适的框架实现 WebService 接口。

标签: 如何写接口
分享给朋友:

相关文章

vue实现接口分页

vue实现接口分页

Vue 实现接口分页的方法 在 Vue 中实现接口分页通常需要结合后端 API 和前端分页组件。以下是常见的实现方式: 使用 Element UI 的分页组件 安装 Element UI 后,可以使…

react 如何写页面跳转

react 如何写页面跳转

页面跳转的实现方式 在React中实现页面跳转可以通过多种方式,以下是常见的几种方法: 使用React Router的<Link>组件 React Router是React生态中最常用的…

react如何写轮播图

react如何写轮播图

使用 React 实现轮播图 轮播图可以通过多种方式实现,以下是几种常见的方法,包括使用第三方库和纯手写实现。 使用第三方库(react-slick) react-slick 是一个流行的轮播图库,…

react共用组件如何请求接口

react共用组件如何请求接口

在React中实现共用组件的接口请求 共用组件的接口请求需要结合组件的复用性和数据独立性进行设计。以下是几种常见方法: 通过props传递数据 父组件负责获取数据并通过props传递给共用组件:…

react中如何写原生js

react中如何写原生js

在 React 中编写原生 JavaScript 可以通过以下几种方式实现,具体取决于使用场景和需求: 直接操作 DOM React 推荐使用虚拟 DOM 进行更新,但在某些情况下需要直接操作真实…

react中如何写less的样式

react中如何写less的样式

在React中使用Less样式 在React项目中集成Less预处理器需要安装相关依赖并进行配置。以下是具体方法: 安装Less依赖 通过npm或yarn安装less和less-loader: n…