Springboot整合cxf进行WebService发布和WebService调用

2:服务端接口

package com.nobody.service;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

/**

  • 测试接口

  • @author Μr.ηobοdy

  • @date 2019-12-29

*/

@WebService(name = “UserService”, // 暴露服务名称

targetNamespace = “http://service.nobody.com” // 命名空间,一般是接口的包名倒序

)

public interface UserService {

@WebMethod

@WebResult(name = “String”, targetNamespace = “”)

String addUser(@WebParam(name = “username”) String username, @WebParam(name = “age”) int age);

}

3:服务端接口实现

package com.nobody.service.impl;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

import com.nobody.service.UserService;

/**

  • 测试接口实现

  • @author Μr.ηobοdy

  • @date 2019-12-29

*/

@WebService(serviceName = “UserService”, // 与接口中指定的name一致

targetNamespace = “http://service.nobody.com”, // 与接口中的命名空间一致,一般是接口的包名倒

endpointInterface = “com.nobody.service.UserService” // 接口地址

)

@Component

public class UserServiceImpl implements UserService {

@Override

public String addUser(String username, int age) {

return “Add user success,username:” + username + “,age:” + age;

}

}

4:CXF配置

package com.nobody.config;

import javax.xml.ws.Endpoint;

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 com.nobody.service.UserService;

/**

  • cxf配置

  • @author Μr.ηobοdy

  • @date 2019-12-29

*/

@Configuration

public class CxfConfig {

@Autowired

private Bus bus;

@Autowired

private UserService userService;

@Bean

public Endpoint endpoint() {

EndpointImpl endpoint = new EndpointImpl(bus, userService);

endpoint.publish(“/UserService”);

return endpoint;

}

}

5:wsdl文件

默认服务在host:port/工程服务名/services/所在的路径下。因为本工程没设置服务名,故此接口发布路径为/services/UserService,wsdl文件路径为:

http://localhost:8080/services/UserService?wsdl

服务启动后,在浏览器输入以上地址即可看到wsdl文件:

在这里插入图片描述

6:客户端调用

package com.nobody.controller;

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

@RestController

@RequestMapping(“demo”)

public class DemoController {

@GetMapping(“test”)

public String test(@RequestParam String username, @RequestParam int age) {

String result = null;

// 创建动态客户端

JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();

Client client = dcf.createClient(“http://localhost:8080/services/UserService?wsdl”);

// 如果需要密码时加上用户名和密码

// client.getOutInterceptors().add(new ClientLoginInterceptor(USERNAME,PASSWORD));

// 接受返回值对象

Object[] objects = new Object[0];

try {

// 调用

react和vue的比较

相同
1)vitual dom
2)组件化
3)props,单一数据流

不同点
1)react是jsx和模板;(jsx可以进行更多的js逻辑和操作)
2)状态管理(react)
3)对象属性(vue)
4)vue:view——medol之间双向绑定
5)vue:组件之间的通信(props,callback,emit)

));

// 接受返回值对象

Object[] objects = new Object[0];

try {

// 调用

react和vue的比较

相同
1)vitual dom
2)组件化
3)props,单一数据流

不同点
1)react是jsx和模板;(jsx可以进行更多的js逻辑和操作)
2)状态管理(react)
3)对象属性(vue)
4)vue:view——medol之间双向绑定
5)vue:组件之间的通信(props,callback,emit)

[外链图片转存中…(img-xOFXwJi3-1718559961186)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值