<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>
package com.primeton.mq.service;
import javax.jws.WebService;
@WebService(name = "DemoService", // 暴露服务名称
targetNamespace = "http://service.mq.primeton.com"// 命名空间,一般是接口的包名倒序
)
public interface DemoService {
public String sayHello(String user);
}
package com.primeton.mq.service.impl;
import com.primeton.mq.service.DemoService;
import javax.jws.WebService;
import java.util.Date;
@WebService(serviceName = "DemoService", // 与接口中指定的name一致
targetNamespace = "http://service.mq.primeton.com", // 与接口中的命名空间一致,一般是接口的包名倒
endpointInterface = "com.primeton.mq.service.DemoService"// 接口地址
)
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String user) {
return user+",现在时间:"+"("+new Date()+")";
}
}
package com.primeton.mq.webServiceConfig;
import com.primeton.mq.service.DemoService;
import com.primeton.mq.service.impl.DemoServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration //1
public class CxfConfig {
@Bean
public ServletRegistrationBean cxrfServlet() { //2
return new ServletRegistrationBean(new CXFServlet(),"/demo/*"); //3
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public DemoService demoService() { //4
return new DemoServiceImpl(); //5
}
@Bean
public Endpoint endpoint() {
EndpointImpl endpoint = new EndpointImpl(springBus(), demoService());
endpoint.publish("/api");//6
return endpoint;
}
}
1:注意不要忘记configuration注解
2:注意命名,不要命名成dispatcherServlet,不然会报错。
3:我觉得就固定写法嘛,反正请求时类似http://localhost:8089/webservice/userService?wsdl。
4:就是你要开放的接口的接口类型。
5:就是你要开放的接口的接口类型的实现类。
6:就是你想要将你的接口暴露出来之后,别人使用时的名字,自己拟定一个。
访问:http://localhost:8090/demo/api?wsdl
package com.swagger.demo.controller;
import com.swagger.demo.model.entity.Code;
import com.swagger.demo.service.CodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* UserController类
*
* <p>
* <b>History:</b>
* <table border="1">
* <tr>
* <th>Date</th>
* <th>Operator</th>
* <th>Memo</th>
* </tr>
* <tr>
* <td>2021/8/25 17:51</td>
* <td>zrc</td>
* <td>Create</td>
* </tr>
* </table>
*
* @author zrc
* @version 1.0.0
* @since 1.0.0
*/
@Api(tags = "热点数据接口")
@RestController
@RequestMapping("codeController")
public class CodeController {
@Autowired
private CodeService codeService;
/**
* 静态变量:系统日志
*/
private static final Log logger = LogFactory.getLog(CodeController.class);
@ApiOperation(value = "测试webservice")
@PostMapping("/testWebservice")
public List<Code> testWebservice() throws InterruptedException {
// 接口地址
String address = "http://localhost:8089/webservice/userService?wsdl";
// 代理工厂
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 设置代理地址
jaxWsProxyFactoryBean.setAddress(address);
// 设置接口类型
jaxWsProxyFactoryBean.setServiceClass(CodeService.class);
// 创建一个代理接口实现
codeService = (CodeService) jaxWsProxyFactoryBean.create();
// 调用代理接口的方法调用并返回结果
List<Code> codes = codeService.getCodes();
System.out.println("返回结果: " + codes);
return codes;
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.4.1</version>
</dependency>
<!--json转换-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.4.1</version>
</dependency>
@WebService
public interface UserService {
@POST
void saveUser(User user);
@PUT
void updateUser(User user);
@DELETE
@Path("/{id}")
void deleteUser(@PathParam("id") Integer id);
@GET
List<User> findAllUser();
@GET
@Path("/{id}")
//转json
@Produces(MediaType.APPLICATION_JSON)
User findById(@PathParam("id")Integer id);
}
实现类跟ws的一样
@Configuration
public class JaxRsConfig {
@Autowired
private Bus bus;
@Autowired
private UserService userService;
@Bean
public Server createdServer(){
JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
//设置访问地址
endpoint.setAddress("/userService");
//设置bus
endpoint.setBus(bus);
//设置实体类对象
endpoint.setServiceBean(userService);
return endpoint.create();
}
}
浏览器访问,如果要别的格式的响应,就到服务端接口上改 @Produces(MediaType.APPLICATION_JSON),自行查看MediaType
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/qq_47109099/article/details/126031108
内容来源于网络,如有侵权,请联系作者删除!