Feign的编解码使用

一 JSON编码器的使用

1 编写服务端的控制器

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {
    @RequestMapping(value = "/person/create", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public String createPerson(@RequestBody Person p) {
        System.out.println(p.getName() + "---" + p.getAge());
        return "success, id: " + p.getId();
    }
    
}

2 编写客户端接口


package org.crazyit.cloud;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import feign.Headers;
import feign.Param;
import feign.RequestLine;

public interface HelloClient {
    @RequestLine("POST /person/createXML")
    @Headers("Content-Type: application/xml")
    public Result createXMLPerson(Person p);

}

3 编写测试方法

package org.crazyit.cloud;

import feign.Feign;
import feign.gson.GsonEncoder;

public class JsonTest {

    public static void main(String[] args) {
        HelloClient client = Feign.builder()
                .encoder(new GsonEncoder())
                .target(HelloClient.class,
                "http://localhost:8080");
        Person p = new Person();
        p.setAge(33);
        p.setId(1);
        p.setName("angus");
        String result = client.createPerson(p);
        System.out.println(result);
    }

}

4 启动服务端

5 测试结果

success, id: 1

二 XML的编码和解密

1 服务端编写

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyRestController {

    /**
     * 参数与返回值均为XML
     */
    @RequestMapping(value = "/person/createXML", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_XML_VALUE,
            produces = MediaType.APPLICATION_XML_VALUE)
    public String createXMLPerson(@RequestBody Person person) {
        System.out.println(person.getName() + "-" + person.getId());
        return "success";
    }
}

2 编写客户端接口

package org.crazyit.cloud;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import feign.Headers;
import feign.Param;
import feign.RequestLine;

public interface HelloClient {
    @RequestLine("POST /person/createXML")
    @Headers("Content-Type: application/xml")
    public Result createXMLPerson(Person p);

}

3 客户端增加依赖

            
                  io.github.openfeign
                  feign-jaxb
                  9.5.0
            
            
                  javax.xml.bind
                  jaxb-api
                  2.2.8
            

4 编写Result

package org.crazyit.cloud;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

@XmlRootElement
public class Result {

    @XmlElement
    private String message;

    @XmlTransient
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
} 

5 编写测试方法

package org.crazyit.cloud;

import feign.Feign;
import feign.jaxb.JAXBContextFactory;
import feign.jaxb.JAXBDecoder;
import feign.jaxb.JAXBEncoder;

public class XmlTest {

    public static void main(String[] args) {
        JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder().build();
        // 获取服务接口
        HelloClient client = Feign.builder()
                .encoder(new JAXBEncoder(jaxbFactory))
                .decoder(new JAXBDecoder(jaxbFactory))
                .target(HelloClient.class, "http://localhost:8080/");
        // 构建参数
        Person p = new Person();
        p.setId(1);
        p.setName("angus");
        p.setAge(33);
        // 调用接口并返回结果
        Result result = client.createXMLPerson(p);
        System.out.println(result.getMessage());
    }

}

6 启动服务

7 测试

客户端输出

success

服务端输出

angus-1

你可能感兴趣的:(微服务)