• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JAVA开发spring RestFull风格Feign使用

武飞扬头像
骇客野人
帮助1

现在大多数的springboot都是使用RestFull风格的接口,是Feign进行远程调用。

学新通

一、Feign介绍:

Feign是Spring Cloud Netflix组件中的一个轻量级RESTFULL的http服务客户端,实现了负载均衡和Rest调用的开源框架,封装了Ribbon和RestTemplate,实现了webservice的面向接口编程,进一步降低了项目的耦合度。
Feign内置了Ribbon,用来做客户端负载均衡调用服务注册中心的服务。
Feign本身并不支持SpringMVC的注解,它有一套自己的注解,为了更方便的使用,Spring Cloud孵化了OpenFeign。
Feign是一种申明式、模板化的HTTP客户端,可以让提供者无感知,消费者申明一下即可。
 

二、RestFull风格:

  1.  
    http://127.0.0.1:8080/stu/student/mozun #查询、get请求
  2.  
    http://127.0.0.1:8080/stu/student/ #新增、post请求
  3.  
    http://127.0.0.1:8080/stu/student/ #删除、put请求
  4.  
    http://127.0.0.1:8080/stu/student/mozun #修改、delete请求

传统风格:url后面直接通过?拼接参数传递

  1.  
    http://127.0.0.1:8080/stu/getStudent?userId=mozun #查询、get请求
  2.  
    http://127.0.0.1:8080/stu/addStudent #新增、post请求
  3.  
    http://127.0.0.1:8080/stu/delStudent?userId=mozun #删除、get或者post请求
  4.  
    http://127.0.0.1:8080/stu/updateStudent #修改、post请求

三、springboot的RestFull风格代码编写:

  1.  
    import java.util.Map;
  2.  
     
  3.  
    import org.springframework.beans.factory.annotation.Autowired;
  4.  
    import org.springframework.web.bind.annotation.PostMapping;
  5.  
    import org.springframework.web.bind.annotation.RequestBody;
  6.  
    import org.springframework.web.bind.annotation.RequestMapping;
  7.  
    import org.springframework.web.bind.annotation.RestController;
  8.  
     
  9.  
    import io.swagger.annotations.ApiOperation;
  10.  
     
  11.  
     
  12.  
    @RestController
  13.  
    @RequestMapping("merchant")
  14.  
    public class MerchantController {
  15.  
     
  16.  
    @Autowired
  17.  
    private MerchantService merchantService;
  18.  
     
  19.  
    @ApiOperation(value = "商户注册", notes = "商户注册")
  20.  
    @PostMapping("register")
  21.  
    public ResponseData<String> register(@RequestBody Map<String, Object> param) {
  22.  
    try {
  23.  
    String str = merchantService.register(param);
  24.  
    return ResponseData.success(str);
  25.  
    } catch (Exception e) {
  26.  
    e.printStackTrace();
  27.  
    }
  28.  
    return ResponseData.error();
  29.  
    }
  30.  
     
  31.  
    @ApiOperation(value = "商户查询", notes = "商户查询")
  32.  
    @PostMapping("select")
  33.  
    public ResponseData<String> select(@RequestBody Map<String, Object> param) {
  34.  
    try {
  35.  
    String str = merchantService.select(param);
  36.  
    return ResponseData.success(str);
  37.  
    } catch (Exception e) {
  38.  
    e.printStackTrace();
  39.  
    }
  40.  
    return ResponseData.error();
  41.  
    }
  42.  
     
  43.  
    @ApiOperation(value = "商户更新", notes = "商户更新")
  44.  
    @PostMapping("updateData")
  45.  
    public ResponseData<String> updateData(@RequestBody Map<String, Object> param) {
  46.  
    try {
  47.  
    String str = merchantService.updateData(param);
  48.  
    return ResponseData.success(str);
  49.  
    } catch (Exception e) {
  50.  
    e.printStackTrace();
  51.  
    }
  52.  
    return ResponseData.error();
  53.  
    }
  54.  
     
  55.  
     
  56.  
    }
学新通

controller需要@RestController ,@RequestMapping 进行注解。

常传递参数的方式注解:

@RequestBody  

@RequestHeader 

@RequestParam

使用方式:

  @RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);

而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。

平时开发接口一般先自己通过postman测试通过后与前端联调,body的传参数方式示例:

学新通

 @RequestParam注解主要参数:

1、value:请求中传入参数的名称,如果不设置后台接口的value值,则会默认为该变量名。比如上图中第一个参数如果不设置value="page",则前端传入的参数名必须为pageNum,否则在后台接口中pageNum将接收不到对应的数据

2、required:该参数是否为必传项。默认是true,表示请求中一定要传入对应的参数,否则会报404错误,如果设置为false时,当请求中没有此参数,将会默认为null,而对于基本数据类型的变量,则必须有值,这时会抛出空指针异常。如果允许空值,则接口中变量需要使用包装类来声明。

3、defaultValue:参数的默认值,如果请求中没有同名的参数时,该变量默认为此值。注意默认值可以使用SpEL表达式,如"#{systemProperties['java.vm.version']}"

如果在请求中传入多个同名参数,比如:url?userName=zhl&userName=holley时怎么办?

其实此时传入的数据格式是:"zhl,holley",即多个数据之间使用逗号分隔开,在后台接口中可以使用数组或者list类型的变量来接收:

public String requestparam8(@RequestParam(value="userName") String []  userNames) 

或者

public String requestparam8(@RequestParam(value="list") List<String> list) 

@RequestHeader用于将请求的头信息区数据映射到功能处理方法的参数上。

  1.  
    @RequestMapping(value="/header")
  2.  
    public String test(
  3.  
    @RequestHeader("User-Agent") String userAgent,
  4.  
    @RequestHeader(value="Accept") String[] accepts)

请求头中写的信息示例:

学新通

接口信息可以通过引入Swagger组件进行预览方便开发:

@EnableSwagger2

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

地址:http://127.0.0.1:8080/系统名r/服务名/swagger-ui.html#/

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgjaegf
系列文章
更多 icon
同类精品
更多 icon
继续加载