例子github地址:https://github.com/zhuquanwen/webservice-demo
其中的cxf模块
1、依赖
我使用的是gradle,使用maven转为对应的pom.xml就好
plugins {
id 'java'
}
group 'com.zqw.test.client'
sourceCompatibility = 1.8
repositories {
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
maven {
url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
}
maven { url 'http://repo.spring.io/plugins-release' }
mavenCentral()
}
//https://blog.csdn.net/wk52525/article/details/79113978
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot
compile group: 'org.springframework.boot', name: 'spring-boot', version: '2.2.1.RELEASE'
// compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.1.RELEASE'
// https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.2.1.RELEASE'
compile 'org.apache.cxf:cxf-spring-boot-starter-jaxrs:3.3.4'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider
compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: '2.8.5'
}
2、定义实体
package com.zqw.test.cxf.model;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
/**
* //TODO
*
* @author zhuquanwen
* @vesion 1.0
* @date 2019/11/10 19:38
* @since jdk1.8
*/
@XmlRootElement
public class Demo {
private Integer id;
private String name;
public Demo() {
}
public Demo(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Demo{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
3、编写接口
package com.zqw.test.cxf.service;
import com.zqw.test.cxf.model.Demo;
import javax.ws.rs.*;
/**
* //TODO
*
* @author zhuquanwen
* @vesion 1.0
* @date 2019/11/10 19:39
* @since jdk1.8
*/
@Path("/demoservice")
public interface IDemoService {
/**
* 客户服务:根据id查询
*/
@Path("/find/{id}")
@GET
@Produces({"application/xml", "application/json"})
Demo findById(@PathParam("id") Integer id);
@Path("/delete/{id}")
@DELETE
@Produces({"application/xml", "application/json"})
boolean deleteById(@QueryParam("id") Integer id);
@Path("/add")
@PUT
@Produces({"application/xml", "application/json"})
boolean add(@FormParam("id") Integer id, @FormParam("name") String name);
@Path("/edit")
@PUT
@Produces({"application/xml", "application/json"})
boolean edit(@FormParam("id") Integer id, @FormParam("name") String name);
}
4、编写实现
package com.zqw.test.cxf.service;
import com.zqw.test.cxf.model.Demo;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* //TODO
*
* @author zhuquanwen
* @vesion 1.0
* @date 2019/11/10 20:29
* @since jdk1.8
*/
@Service
public class DemoService implements IDemoService {
public static List demoList = new ArrayList<>();
static {
demoList = Arrays.asList(
new Demo(0, "name1"),
new Demo(1, "name2"),
new Demo(2, "name3")
);
}
@Override
public Demo findById(Integer id) {
return demoList.get(id);
}
@Override
public boolean deleteById(Integer id) {
return demoList.remove(id);
}
@Override
public boolean add(Integer id, String name) {
Demo demo = new Demo(id, name);
return demoList.add(demo);
}
@Override
public boolean edit(Integer id, String name) {
Demo demo = demoList.get(id);
demo.setName(name);
return true;
}
}
5、客户端配置JSON转化工具
由于WebClient的create()方法需要的是List形式的参数,所以创建一个继承ArrayList类的JsonProvider,在构造方法中添加JacksonJaxbJsonProvider对象元素
package com.zqw.test.cxf.client;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@Component
public class JsonProvider extends ArrayList {
// 在构造方法中, 添加JacksonJaxbJsonProvider
public JsonProvider() {
this.add(new JacksonJaxbJsonProvider());
}
}
6、编写调用客户端
package com.zqw.test.cxf.client;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.zqw.test.cxf.model.Demo;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.ws.rs.core.MediaType;
import java.util.List;
/**
* //TODO
*
* @author zhuquanwen
* @vesion 1.0
* @date 2019/11/10 20:50
* @since jdk1.8
*/
@RestController
public class TestController {
// 注入配置的转json工具
@Autowired
private List jsonProvider;
@RequestMapping("/findById")
@ResponseBody
public Demo findById() {
//调用webservice获取查询数据
Demo demo = WebClient
.create("http://localhost:9001/services/demoservice/find/1", jsonProvider)
.accept(MediaType.APPLICATION_JSON).get(Demo.class);
return demo;
}
}
8、编写启动类
package com.zqw.test.cxf;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
/**
* //TODO
*
* @author zhuquanwen
* @vesion 1.0
* @date 2019/11/10 19:35
* @since jdk1.8
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
// 配置一个对象与json转换的工具
@Bean
public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {
return new JacksonJaxbJsonProvider();
}
}
9、配置文件
server:
port: 9001
cxf:
path: /services
servlet.init:
service-list-path: /info
jaxrs:
component-scan: true
10、测试
启动后访问http://localhost:9001/findById