OpenFeign生产者与消费者

用到的VO

package com.datang.api.vo;

import java.util.List;

/**
 * @author 顶风少年
 * @date 2022/6/4
 */
public class Organization {

    public Organization() {
    }

    public Organization(String name, List<User> userList) {
        this.name = name;
        this.userList = userList;
    }

    private String name;

    private List<User> userList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    @Override
    public String toString() {
        return "Organization{" +
                "name='" + name + '\'' +
                ", userList=" + userList +
                '}';
    }
}
View Code
package com.datang.api.vo;

/**
 * @author 顶风少年
 * @date 2022/6/1
 */

public class User {
    private String name;
    private Integer age;

    public User() {
    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
View Code

GET生产者

package com.datang.business.controller;

import com.datang.business.vo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author 顶风少年
 * @date 2022/6/1
 */
@RestController
public class GetController {

    @GetMapping(value = "g1")
    public String g1() {
        return "g1";
    }

    @GetMapping(value = "g2/{name}/{age}")
    public String g2(@PathVariable(value = "name") String name, @PathVariable(value = "age") int age) {
        return name + "--------" + age;
    }

    @GetMapping(value = "g3")
    public String g3(String name, Integer age) {
        return name + "~~~~~~~" + age;
    }


    @GetMapping(value = "g4")
    public String g4(User user,String name2) {
        return user.toString()+"!!!!!!!!!!!!!!"+name2;
    }

    //这种情况下name2会映射到参数,但map中也会有name2
    @GetMapping(value = "g5")
    public String g5(@RequestParam Map<String, String> map,String name2) {
        return map.toString()+"!!!!!!!!!!!!!!"+name2;
    }

    @GetMapping(value = "g6")
    public String g6(@RequestParam List<String> list,String name) {
        return list.toString()+"~~~~~~~~"+name;
    }

    @GetMapping(value = "g7")
    public String g7(String[] arr,String name) {
        return arr.toString()+"~~~~~~~~"+name;
    }
}
View Code

GET消费者

package com.datang.api.service;

import com.datang.api.vo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author 顶风少年
 * @date 2022/6/1
 */
@FeignClient("business")
public interface GetBusinessService {
    @GetMapping(value = "g1")
    public String g1();

    @GetMapping(value = "g2/{name}/{age}")
    public String g2(@PathVariable(value = "name") String name, @PathVariable(value = "age") int age);

    @GetMapping(value = "g3")
    public String g3(@RequestParam String name, @RequestParam Integer age);

    @GetMapping(value = "g4")
    public String g4(@SpringQueryMap User user,@RequestParam String name2);

    @GetMapping(value = "g5")
    public String g5(@RequestParam Map<String, String> map,@RequestParam String name2);

    @GetMapping(value = "g6")
    public String g6(@RequestParam List<String> list,@RequestParam String name);

    @GetMapping(value = "g7")
    public String g7(@RequestParam String[] arr,@RequestParam String name) ;
}
View Code

GET测试

package com.datang.api.controller;

import com.datang.api.service.GetBusinessService;
import com.datang.api.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 顶风少年
 * @date 2022/6/1
 */
@RestController
@RequestMapping(value = "g/")
public class GetController {


    @Autowired
    private GetBusinessService businessService;

    @GetMapping(value = "test1")
    public String test1() {
        return businessService.g1();
    }

    @GetMapping(value = "test2")
    public String test2() {
        return businessService.g2("zhangsan", 11);
    }

    @GetMapping(value = "test3")
    public String test3() {
        return businessService.g3("zhangsan", 11);
    }

    @GetMapping(value = "test4")
    public String test4() {
        User u = new User();
        u.setAge(1);
        u.setName("aaaaaa");
        return businessService.g4(u,"bbbbbbbb");
    }

    @GetMapping(value = "test5")
    public String test5() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "111");
        map.put("age", "222");
        return businessService.g5(map,"ggggggggg");
    }

    @GetMapping(value = "test6")
    public String test6() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");

        User u = new User();
        u.setAge(1);
        u.setName("aaaaaa");
        return businessService.g6(list,"qqqqqqqqqqqq");
    }

    @GetMapping(value = "test7")
    public String test7() {
        String[] arr = {"a","b","c"};
        return businessService.g7(arr,"qqqqqqqqqqqq");
    }
}
View Code

POST生产者

package com.datang.business.controller;

import com.datang.business.vo.Organization;
import com.datang.business.vo.User;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 顶风少年
 * @date 2022/6/1
 */
@RestController
public class PostController {
    @PostMapping(value = "p1")
    public String p1() {
        return "p1";
    }

    @PostMapping(value = "p2")
    public String p2(@RequestBody User user) {
        return user.toString();
    }

    @PostMapping(value = "p3")
    public String p3(String name, Integer age) {
        return name + "~~~~~~~" + age;
    }


    @PostMapping(value = "p4")
    public String p4(@RequestBody Organization organization, String name) {
        return organization.toString() + "@@@@@@@@@@" + name;
    }

    @PostMapping(value = "p5")
    public String p5(@RequestBody Map<String, Organization> map, String name) {
        return map.toString() + "@@@@@@@@@@" + name;
    }

    @PostMapping(value = "p6")
    public String p6(@RequestBody List<Organization> list, String name) {
        return list.toString() + "@@@@@@@@@@" + name;
    }

    @PostMapping(value = "p7")
    public String p7(@RequestBody Organization[] arr, String name) {
        return arr.toString() + "@@@@@@@@@@" + name;
    }

}
View Code

POST消费者

package com.datang.api.service;

import com.datang.api.vo.Organization;
import com.datang.api.vo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 顶风少年
 * @date 2022/6/1
 */
@FeignClient("business")
public interface PostBusinessService {
    @PostMapping(value = "p1")
    public String p1();

    @PostMapping(value = "p2")
    public String p2(@RequestBody User user);

    @PostMapping(value = "p3")
    public String p3(@RequestParam String name, @RequestParam Integer age);


    @PostMapping(value = "p4")
    public String p4(@RequestBody Organization organization, @RequestParam String name);

    @PostMapping(value = "p5")
    public String p5(@RequestBody Map<String, Organization> map, @RequestParam String name);

    @PostMapping(value = "p6")
    public String p6(@RequestBody List<Organization> list, @RequestParam String name);

    @PostMapping(value = "p7")
    public String p7(@RequestBody Organization[] arr, @RequestParam String name);
}
View Code

POST测试

package com.datang.api.controller;

import com.datang.api.service.PostBusinessService;
import com.datang.api.vo.Organization;
import com.datang.api.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 顶风少年
 * @date 2022/6/1
 */
@RestController
@RequestMapping(value = "p/")
public class PostController {


    @Autowired
    private PostBusinessService businessService;

    @GetMapping(value = "test1")
    public String test1() {
        return businessService.p1();
    }

    @GetMapping(value = "test2")
    public String test2() {
        User user = new User("aaaa", 11);
        return businessService.p2(user);
    }

    @GetMapping(value = "test3")
    public String test3() {
        return businessService.p3("aaaa", 11);
    }



    @GetMapping(value = "test4")
    public String test4() {
        List<User> userList = new ArrayList<>();
        User user = new User("aaa", 1111);
        User user2 = new User("bbbb", 2222);
        userList.add(user);
        userList.add(user2);
        Organization organization = new Organization("ttttt", userList);
        return businessService.p4(organization, "yyyyyyyyyy");
    }

    @GetMapping(value = "test5")
    public String test5() {
        List<User> userList = new ArrayList<>();
        User user = new User("aaa", 1111);
        User user2 = new User("bbbb", 2222);
        userList.add(user);
        userList.add(user2);
        Organization organization = new Organization("ttttt", userList);

        Map<String, Organization> map = new HashMap<>();
        map.put("aa",organization);
        return businessService.p5(map,"tttttttt");
    }

    @GetMapping(value = "test6")
    public String test6() {
        List<User> userList = new ArrayList<>();
        User user = new User("aaa", 1111);
        User user2 = new User("bbbb", 2222);
        userList.add(user);
        userList.add(user2);
        Organization organization = new Organization("ttttt", userList);

        List<Organization> organizationList = new ArrayList<>();
        organizationList.add(organization);
        organizationList.add(organization);

        return businessService.p6(organizationList,"tttttttt");
    }

    @GetMapping(value = "test7")
    public String test7() {
        List<User> userList = new ArrayList<>();
        User user = new User("aaa", 1111);
        User user2 = new User("bbbb", 2222);
        userList.add(user);
        userList.add(user2);
        Organization organization = new Organization("ttttt", userList);

        Organization[] organizationArr = {organization,organization};

        return businessService.p7(organizationArr,"tttttttt");
    }

}
View Code

PUT和DELETE和POST的参数都是在请求体的Body中,所以是一样的。

 

转发一份大佬写的配置head的方案挺全乎。https://www.jianshu.com/p/6fbf40e1ed1b

 

posted @ 2022-06-04 20:34  顶风少年  阅读(127)  评论(0编辑  收藏  举报
返回顶部