如何发布webservice java
使用JAX-WS发布WebService
在Java中发布WebService可以通过JAX-WS(Java API for XML Web Services)实现。JAX-WS是Java EE的一部分,简化了WebService的创建和部署。
- 创建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;
}
}
- 发布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>
- 创建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框架,提供了更多高级功能。
-
添加依赖
<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> -
配置和发布服务
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访问。






