SpringCloud之Feign传递Json参数(个人使用)

2023-11-10

SpringCloud之Feign传递Json参数(个人使用)

Client端:

启动类:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignClientApplication {
    public static void main(String[] args) {
       SpringApplication.run(FeignClientApplication.class, args);
    }
}

controller:

@RestController
@RequestMapping("/HelloController")
public class HelloController {
    private Logger logger = LoggerFactory.getLogger(getClass());
     
    @Autowired
    private HelloService helloService;
     
    @RequestMapping(value = "/hello1", method = RequestMethod.GET)
    public String hello1(@RequestHeader("username") String username, @RequestHeader("phone") String phone) {
       HelloPojo helloPojo = new HelloPojo();
       helloPojo.setUsername(username);
       helloPojo.setPhone(phone);
       String json = JSONObject.toJSONString(helloPojo);
       return helloService.select1(json);
    }
}

service:

@FeignClient(value = "SpringCloud-Feign-Server")
@RequestMapping("/HelloController")
public interface HelloService {
   
    @RequestMapping(value = "/hello/select1", method = RequestMethod.POST,consumes = "application/json")
    String select1(@RequestParam("json") String json);
}

pojo:

public class HelloPojo implements Serializable {
    private String username;
    private String phone;

    public String getUsername() {
       return username;
    }

    public void setUsername(String username) {
       this.username = username;
    }

    public String getPhone() {
       return phone;
    }

    public void setPhone(String phone) {
       this.phone = phone;
    }

}

application.yml

spring:
  application:
    name: SpringCloud-Feign-Client
eureka:
  client:
    service-url:
     defaultZone: http://localhost:8761/eureka
  instance:
    instance-id:  ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
server:
  port: 9090

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!-- spring boot test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.4.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.4</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RC1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

 

server端:

启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class FeignServerApplication {
    public static void main(String[] args) {
       SpringApplication.run(FeignServerApplication.class, args);
    }
}

controller:

@RestController
public class HelloController implements HelloService {
    private Logger logger = LoggerFactory.getLogger(getClass());
   
    @Override
    public String select1(String json) {
       logger.info("SpringCloud-Feign-Server》》》》method--->select1:" + json);
       return "SpringCloud-Feign-Server》》》》method--->select1:" + json;
    }
}

interface:

@RequestMapping("/HelloController")
public interface HelloService {
    @RequestMapping(value = "/hello/select1", method = RequestMethod.POST,consumes = "application/json")
    String select1(@RequestParam("json") String json);
}

pojo:

public class HelloPojo implements Serializable {
    private String username;
    private String phone;

    public String getUsername() {
       return username;
    }

    public void setUsername(String username) {
       this.username = username;
    }

    public String getPhone() {
       return phone;
    }

    public void setPhone(String phone) {
       this.phone = phone;
    }

}

application.yml


server:
  port: 9051
 
spring:
  application:
    name: SpringCloud-Feign-Server
   
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
  instance:
    instance-id:  ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!-- spring boot test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.RC1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringCloud之Feign传递Json参数(个人使用) 的相关文章

随机推荐

  • 深度学习之数学基础(线性代数篇)

    2 1 标量 一个标量就是一个单独的数 一般用小写的的变量名称表示 2 2 向量 一个向量就是一列数 这些数是有序排列的 用过次序中的索引 我们可以确定每个单独的数 通常会赋予向量粗体的小写名称 当我们需要明确表示向量中的元素时 我们会将元
  • 【计算机网络】1.5——计算机网络的体系结构(网络分层模型)

    计算机网络的体系结构 概述 计算机网络的体系结构是计算机网络及其构建所应完成功能的精确定义 考题 不属于网络体系结构所描述的内容的是 A 网络的层次 B 每层使用的协议 C 协议的内部实现细节 D 每层必须完成的功能 这些功能的 实现细节
  • html + css实现点赞特效

    html
  • BUUCTF做题Upload-Labs记录pass-01~pass-10

    BUUCTF做题Upload Labs记录pass 01 pass 10 pass 01 前端验证后缀 传1 png 抓包改1 php 即可 蚁剑连 http your ip upload 1 php pass 02 验证content t
  • 前端部署项目到服务器

    1 通过xshell配置 1 1下载xshell 并安装 1 2配置root以及服务器地址 1 3配置nginx 路径设置为xxx xxx dist 1 4npm run build 将打包的dist文件放入配置路径下 1 5无法拖动文件处
  • 文本自动分类

    使用机器学习方法 做文档的自动分类 套路 1 根据每个文件 生成该文件的一个特征 2 根据特征 选择 分类器 进行文本分类 3 可选 根据 2 步结果 调整参数 特征等 示例 数据 搜狗文本分类语料库 精简版 分类器 朴素贝叶斯 编程语言
  • Windows防火墙配置(允许某个网段和部分IP访问某个端口)

    1 win R 2 gpedit msc 3 计算机配置 Windows设置 安全设置 IP安全策略 在本地计算机 4 创建IP安全策略 5 配置IP筛选器列表 筛选器操作 6 分配 https img2018 cnblogs com bl
  • Transformers使用教程

    模型参数下载 数据集查找 Transformers中文使用说明 Huggingface 超详细介绍 知乎 Hugging Face 的 Transformers 库快速入门 文档说明 Transformers 提供文本相关的预训练模型 Di
  • (一)linux系统安装——从0开始大数据开发实战:电影推荐系统(scala版)

    参考资源 厦大实验室博客http dblab xmu edu cn blog 大数据基础编程 实验和案例教程 林子雨 linux版本 ubuntu ubuntukylin 16 04 desktop amd64 https pan baid
  • OpenCV三维图像的创建和数据遍历

    创建一个如图所示的3 X 4 X 6 三维矩阵 include
  • 学习iot_小熊派IoT开发板系列教程正式发布——免费学习

    小宅按 小熊派开源社区针对小熊派IoT开发板首次规划了小熊派未来的系列教程 从基础到进阶的设计 可适应具有不同基础的开发者 通过该系列教程的学习 开发者能够轻松掌握IoT产品的开发 该系列教程包括单片机基础 LiteOS操作系统基础 通信外
  • [第五空间 2021]pklovecloud

    除了按部就班的根据代码来写序列化
  • 42 访问者模式(Visitor模式)详解

    行为型模式 模板方法 Template Method 模式 策略 Strategy 模式 命令 Command 模式 职责链 Chain of Responsibility 模式 状态 State 模式 观察者 Observer 模式 中介
  • stm32---基本定时器(TIM6,TIM7)

    STM32F1的定时器非常多 由两个基本定时器 TIM6 TIM7 4个通用定时器 TIM2 TIM5 和两个高级定时器 TIM TIM 组成 基本定时器的功能最为简单 类似于51单片机内定时器 通用定时器是在基本定时器的基础上扩展而来 增
  • dump文件分析工具_jvm系列:dump文件深度分析

    JVM dump java内存dump是jvm运行时内存的一份快照 利用它可以分析是否存在内存浪费 可以检查内存管理是否合理 当发生OOM的时候 可以找出问题的原因 那么dump文件的内容是什么样的呢 我们一步一步来 获取JVM dump文
  • super()

    super 关键字 关键字的理解 父类中定义了该方法 但是子类中重写了该方法 使用super来修饰这个方法 在调用的时候调用父类的方法 super关键字的使用 1 super理解为父类 2 super可以用来调用 属性方法和构造器 3 su
  • 【Maven】Maven slf4j-api 出现 NoClassDefFoundError:org/slf4j/event/LoggingEvent

    1 背景 程序偶然报错这个 然后我查找了一下 发现这个包是1 7 7版本的 然后我想知道那个版本加入了这个东东 于是去查了一下 可以发现 在1 7 14版本之前都没有和这个包 1 7 14之后就全都有了 换个版本就好了
  • 【速度收藏】20个常用的Python技巧,太赞啦

    Python的可读性和简单性是其广受欢迎的两大原因 本文介绍20个常用的Python技巧来提高代码的可读性 并能帮助你节省大量时间 下面的技巧将在你的日常编码练习中非常实用 1 字符串反转 使用Python切片反转字符串 Reversing
  • 02-express安装apidoc生成接口文档

    02 express安装apidoc生成接口文档 1 安装 npm i apidoc 2 在项目根目录下创建 apidoc json name news是项目接口文档 version 0 1 0 description 新闻接口文档 tit
  • SpringCloud之Feign传递Json参数(个人使用)

    SpringCloud之Feign传递Json参数 个人使用 Client端 启动类 SpringBootApplication EnableDiscoveryClient EnableFeignClientspublic class Fe