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

<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 框架。

添加 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:

java如何写webservice接口

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中实现搜索框接口需要结合前端输入处理和后端API交互。以下是关键实现方法: 数据绑定与输入处理 使用v-model绑定搜索框输入值: <template&g…

vue实现按需调用接口

vue实现按需调用接口

Vue 中按需调用接口的实现方法 在 Vue 项目中按需调用接口可以优化性能,避免不必要的网络请求。以下是几种常见实现方式: 使用计算属性触发接口调用 通过计算属性监听数据变化,在需要时调用接口:…

答辩vue接口如何实现

答辩vue接口如何实现

Vue 接口实现的核心方法 使用 Axios 或 Fetch 发起 HTTP 请求 Axios 是 Vue 项目中常用的 HTTP 客户端,需先通过 npm install axios 安装。在组件中…

react插件如何写

react插件如何写

React 插件开发基础 React 插件通常以 npm 包形式发布,核心是通过封装可复用的组件或逻辑供其他项目调用。需要遵循 React 组件设计规范,并考虑兼容性、性能优化和文档完整性。 项目初…

react 如何写页面跳转

react 如何写页面跳转

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

react如何写轮播图

react如何写轮播图

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