增加一个cxf
This commit is contained in:
@ -0,0 +1,12 @@
|
||||
package com.xncoding.webservice;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
package com.xncoding.webservice.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 com.xncoding.webservice.service.ICommonService;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 默认服务在 Host:port/services/*** 路径下
|
||||
* 这里相当于把Commonservice接口发布在了路径/services/CommonService下
|
||||
* wsdl文档路径为http://localhost:8080/services/CommonService?wsdl
|
||||
*
|
||||
* @author XiongNeng
|
||||
* @version 1.0
|
||||
* @since 2018/6/15
|
||||
*/
|
||||
@Configuration
|
||||
public class CxfConfig {
|
||||
@Autowired
|
||||
private Bus bus;
|
||||
|
||||
@Autowired
|
||||
ICommonService commonService;
|
||||
|
||||
/**
|
||||
* JAX-WS
|
||||
**/
|
||||
@Bean
|
||||
public Endpoint endpoint() {
|
||||
EndpointImpl endpoint = new EndpointImpl(bus, commonService);
|
||||
endpoint.publish("/CommonService");
|
||||
return endpoint;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package com.xncoding.webservice.model;
|
||||
|
||||
/**
|
||||
* 基础返回类
|
||||
*
|
||||
* @author XiongNeng
|
||||
* @version 1.0
|
||||
* @since 2018/1/7
|
||||
*/
|
||||
public class BaseResponse<T> {
|
||||
/**
|
||||
* 是否成功
|
||||
*/
|
||||
private boolean success;
|
||||
|
||||
/**
|
||||
* 说明
|
||||
*/
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 返回数据
|
||||
*/
|
||||
private T data;
|
||||
|
||||
public BaseResponse() {
|
||||
|
||||
}
|
||||
|
||||
public BaseResponse(boolean success, String msg, T data) {
|
||||
this.success = success;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,47 @@
|
||||
package com.xncoding.webservice.model;
|
||||
|
||||
/**
|
||||
* 用户
|
||||
*
|
||||
* @author XiongNeng
|
||||
* @version 1.0
|
||||
* @since 2018/3/4
|
||||
*/
|
||||
public class User {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(Long id, String name, Integer age) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.xncoding.webservice.service;
|
||||
|
||||
import com.xncoding.webservice.model.User;
|
||||
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebParam;
|
||||
import javax.jws.WebService;
|
||||
|
||||
/**
|
||||
* ICommonService
|
||||
*
|
||||
* @author XiongNeng
|
||||
* @version 1.0
|
||||
* @since 2018/6/15
|
||||
*/
|
||||
@WebService(name = "CommonService", // 暴露服务名称
|
||||
targetNamespace = "http://model.webservice.xncoding.com/"// 命名空间,一般是接口的包名倒序
|
||||
)
|
||||
public interface ICommonService {
|
||||
@WebMethod
|
||||
// @WebResult(name = "String", targetNamespace = "")
|
||||
public String sayHello(@WebParam(name = "userName") String name);
|
||||
|
||||
@WebMethod
|
||||
// @WebResult(name = "String", targetNamespace = "")
|
||||
public User getUser(@WebParam(name = "userName") String name);
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.xncoding.webservice.service.impl;
|
||||
|
||||
import com.xncoding.webservice.model.User;
|
||||
import com.xncoding.webservice.service.ICommonService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.jws.WebService;
|
||||
|
||||
/**
|
||||
* CommonServiceImpl
|
||||
*
|
||||
* @author XiongNeng
|
||||
* @version 1.0
|
||||
* @since 2018/6/15
|
||||
*/
|
||||
@WebService(serviceName = "CommonService", // 与接口中指定的name一致
|
||||
targetNamespace = "http://model.webservice.xncoding.com/", // 与接口中的命名空间一致,一般是接口的包名倒
|
||||
endpointInterface = "com.xncoding.webservice.service.ICommonService"// 接口地址
|
||||
)
|
||||
@Component
|
||||
public class CommonServiceImpl implements ICommonService {
|
||||
|
||||
@Override
|
||||
public String sayHello(String name) {
|
||||
return "Hello ," + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User getUser(String name) {
|
||||
return new User(1000L, name, 23);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,45 @@
|
||||
package com.xncoding.webservice.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* JacksonUtil
|
||||
*
|
||||
* @author XiongNeng
|
||||
* @version 1.0
|
||||
* @since 2018/3/4
|
||||
*/
|
||||
public class JacksonUtil {
|
||||
private static ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
public static String bean2Json(Object obj) {
|
||||
try {
|
||||
return mapper.writeValueAsString(obj);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
|
||||
// try {
|
||||
// return mapper.readValue(jsonStr, objClass);
|
||||
// } catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
// return null;
|
||||
// }
|
||||
// }
|
||||
|
||||
public static <T> T json2Bean(String jsonStr, TypeReference<T> typeReference) {
|
||||
try {
|
||||
return mapper.readValue(jsonStr, typeReference);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
33
springboot-cxf/src/main/resources/application.yml
Normal file
33
springboot-cxf/src/main/resources/application.yml
Normal file
@ -0,0 +1,33 @@
|
||||
##########################################################
|
||||
################## 所有profile共有的配置 #################
|
||||
##########################################################
|
||||
|
||||
################### 项目启动端口 ###################
|
||||
server.port: 8092
|
||||
|
||||
################### spring配置 ###################
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
||||
|
||||
cxf:
|
||||
path: /services # 替换默认的/services路径
|
||||
|
||||
logging:
|
||||
level:
|
||||
org.springframework.web.servlet: ERROR
|
||||
|
||||
---
|
||||
|
||||
#####################################################################
|
||||
######################## 开发环境profile ##########################
|
||||
#####################################################################
|
||||
spring:
|
||||
profiles: dev
|
||||
|
||||
logging:
|
||||
level:
|
||||
ROOT: INFO
|
||||
com:
|
||||
xncoding: DEBUG
|
||||
file: E:/logs/app.log
|
||||
23
springboot-cxf/src/main/resources/banner.txt
Normal file
23
springboot-cxf/src/main/resources/banner.txt
Normal file
@ -0,0 +1,23 @@
|
||||
|
||||
_____ _______ _____ _____
|
||||
/\ \ /::\ \ /\ \ /\ \
|
||||
/::\____\ /::::\ \ /::\____\ /::\ \
|
||||
/:::/ / /::::::\ \ /:::/ / /::::\ \
|
||||
/:::/ / /::::::::\ \ /:::/ / /::::::\ \
|
||||
/:::/ / /:::/~~\:::\ \ /:::/ / /:::/\:::\ \
|
||||
/:::/ / /:::/ \:::\ \ /:::/____/ /:::/__\:::\ \
|
||||
/:::/ / /:::/ / \:::\ \ |::| | /::::\ \:::\ \
|
||||
/:::/ / /:::/____/ \:::\____\ |::| | _____ /::::::\ \:::\ \
|
||||
/:::/ / |:::| | |:::| | |::| | /\ \ /:::/\:::\ \:::\ \
|
||||
/:::/____/ |:::|____| |:::| | |::| | /::\____\/:::/__\:::\ \:::\____\
|
||||
\:::\ \ \:::\ \ /:::/ / |::| | /:::/ /\:::\ \:::\ \::/ /
|
||||
\:::\ \ \:::\ \ /:::/ / |::| | /:::/ / \:::\ \:::\ \/____/
|
||||
\:::\ \ \:::\ /:::/ / |::|____|/:::/ / \:::\ \:::\ \
|
||||
\:::\ \ \:::\__/:::/ / |:::::::::::/ / \:::\ \:::\____\
|
||||
\:::\ \ \::::::::/ / \::::::::::/____/ \:::\ \::/ /
|
||||
\:::\ \ \::::::/ / ~~~~~~~~~~ \:::\ \/____/
|
||||
\:::\ \ \::::/ / \:::\ \
|
||||
\:::\____\ \::/____/ \:::\____\
|
||||
\::/ / ~~ \::/ /
|
||||
\/____/ \/____/
|
||||
|
||||
@ -0,0 +1,97 @@
|
||||
package com.xncoding.webservice;
|
||||
|
||||
import com.xncoding.webservice.model.User;
|
||||
import com.xncoding.webservice.service.ICommonService;
|
||||
import org.apache.cxf.endpoint.Client;
|
||||
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
|
||||
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.context.embedded.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class ApplicationTests {
|
||||
@LocalServerPort
|
||||
private Integer port;
|
||||
/**
|
||||
* 接口地址
|
||||
*/
|
||||
private String wsdlAddress;
|
||||
|
||||
@Before
|
||||
public void prepare() {
|
||||
wsdlAddress = "http://localhost:" + port + "/services/CommonService?wsdl";
|
||||
}
|
||||
|
||||
/**
|
||||
* 方式1.代理类工厂的方式,需要拿到对方的接口
|
||||
*/
|
||||
@Test
|
||||
public void cl1() {
|
||||
try {
|
||||
// 接口地址
|
||||
// 代理工厂
|
||||
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
|
||||
// 设置代理地址
|
||||
jaxWsProxyFactoryBean.setAddress(wsdlAddress);
|
||||
// 设置接口类型
|
||||
jaxWsProxyFactoryBean.setServiceClass(ICommonService.class);
|
||||
// 创建一个代理接口实现
|
||||
ICommonService cs = (ICommonService) jaxWsProxyFactoryBean.create();
|
||||
// 数据准备
|
||||
String userName = "Leftso";
|
||||
// 调用代理接口的方法调用并返回结果
|
||||
String result = cs.sayHello(userName);
|
||||
System.out.println("返回结果:" + result);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 方式2. 动态调用方式
|
||||
*/
|
||||
@Test
|
||||
public void cl2() {
|
||||
// 创建动态客户端
|
||||
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
|
||||
Client client = dcf.createClient(wsdlAddress);
|
||||
// 需要密码的情况需要加上用户名和密码
|
||||
// client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
|
||||
Object[] objects;
|
||||
try {
|
||||
// invoke("方法名",参数1,参数2,参数3....);
|
||||
objects = client.invoke("sayHello", "Leftso");
|
||||
System.out.println("返回类型:" + objects[0].getClass());
|
||||
System.out.println("返回数据:" + objects[0]);
|
||||
} catch (java.lang.Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 方式2. 动态调用方式,返回对象User
|
||||
*/
|
||||
@Test
|
||||
public void cl3() {
|
||||
// 创建动态客户端
|
||||
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
|
||||
Client client = dcf.createClient(wsdlAddress);
|
||||
Object[] objects;
|
||||
try {
|
||||
// invoke("方法名",参数1,参数2,参数3....);
|
||||
objects = client.invoke("getUser", "张三");
|
||||
System.out.println("返回类型:" + objects[0].getClass());
|
||||
System.out.println("返回数据:" + objects[0]);
|
||||
User user = (User) objects[0];
|
||||
System.out.println("返回对象User.name=" + user.getName());
|
||||
} catch (java.lang.Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user