本文主要是介绍springboot学习(二十四) springboot整合cxf jaxrs发布webservice使用JSON并调用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例子github地址:https://github.com/zhuquanwen/webservice-demo
其中的cxf模块
1、依赖
我使用的是gradle,使用maven转为对应的pom.xml就好
plugins {id 'java'
}group 'com.zqw.test.client'sourceCompatibility = 1.8repositories {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-bootcompile 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-testtestCompile 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-providercompile 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;}@Overridepublic 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<Demo> demoList = new ArrayList<>();static {demoList = Arrays.asList(new Demo(0, "name1"),new Demo(1, "name2"),new Demo(2, "name3"));}@Overridepublic Demo findById(Integer id) {return demoList.get(id);}@Overridepublic boolean deleteById(Integer id) {return demoList.remove(id);}@Overridepublic boolean add(Integer id, String name) {Demo demo = new Demo(id, name);return demoList.add(demo);}@Overridepublic 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> {// 在构造方法中, 添加JacksonJaxbJsonProviderpublic 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工具@Autowiredprivate List<JacksonJaxbJsonProvider> jsonProvider;@RequestMapping("/findById")@ResponseBodypublic 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转换的工具@Beanpublic JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {return new JacksonJaxbJsonProvider();}
}
9、配置文件
server:port: 9001
cxf:path: /servicesservlet.init:service-list-path: /infojaxrs:component-scan: true
10、测试
启动后访问http://localhost:9001/findById
这篇关于springboot学习(二十四) springboot整合cxf jaxrs发布webservice使用JSON并调用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!