java如何写webservice接口
使用 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:
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 接口。






