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.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<Demo> 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> {

    // 在構造方法中, 添加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<JacksonJaxbJsonProvider> 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章