springmvc项目用cxf框架开发webservice的demo
时间: 2023-08-10 17:23:03 浏览: 51
好的,以下是一个简单的Spring MVC项目,使用CXF框架开发Web Service的示例:
1. 创建一个Maven项目,添加以下依赖:
```
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
```
2. 创建一个接口,定义WebService的方法:
```
package com.example.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWorldService {
@WebMethod
String sayHello(@WebParam(name = "name") String name);
}
```
3. 创建一个实现类,实现接口中的方法:
```
package com.example.webservice;
import javax.jws.WebService;
@WebService(endpointInterface = "com.example.webservice.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String sayHello(String name) {
return "Hello " + name + "!";
}
}
```
4. 在Spring的配置文件中,添加以下配置:
```
<jaxws:endpoint id="helloWorldService"
implementor="com.example.webservice.HelloWorldServiceImpl"
address="/hello"/>
```
5. 创建一个Controller类,在其中注入WebService:
```
package com.example.controller;
import com.example.webservice.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloWorldController {
@Autowired
private HelloWorldService helloWorldService;
@RequestMapping("/hello")
@ResponseBody
public String sayHello(String name) {
return helloWorldService.sayHello(name);
}
}
```
6. 启动项目,在浏览器中访问http://localhost:8080/hello?name=world ,即可看到输出:Hello world!
这就是一个简单的Spring MVC项目,使用CXF框架开发Web Service的示例。