JAVA开发(spring RestFull风格Feign使用总结)

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

一、Feign介绍:

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

二、RestFull风格:

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

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

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

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

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.ApiOperation;

 
@RestController
@RequestMapping("merchant")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;
    
	@ApiOperation(value = "商户注册", notes = "商户注册")
    @PostMapping("register")
    public ResponseData<String> register(@RequestBody Map<String, Object> param) {
        try {
			String  str = merchantService.register(param);
			return ResponseData.success(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return ResponseData.error();
    }
	
	@ApiOperation(value = "商户查询", notes = "商户查询")
    @PostMapping("select")
    public ResponseData<String> select(@RequestBody Map<String, Object> param) {
        try {
			String  str = merchantService.select(param);
			return ResponseData.success(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return ResponseData.error();
    }
	
	@ApiOperation(value = "商户更新", notes = "商户更新")
    @PostMapping("updateData")
    public ResponseData<String> updateData(@RequestBody Map<String, Object> param) {
        try {
			String  str = merchantService.updateData(param);
			return ResponseData.success(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return ResponseData.error();
    }
	
 
}

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用于将请求的头信息区数据映射到功能处理方法的参数上。

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

请求头中写的信息示例:

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

@EnableSwagger2

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

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

猜你喜欢

转载自blog.csdn.net/dongjing991/article/details/131328415
今日推荐