(Solved) How to set the request header and incoming parameters when calling the Fegin interface

Problem phenomenon:

Recently, I am studying how to use Fegin to call third-party service interfaces and set request headers and parameters. I found the characteristics of method annotation @Header, parameter attribute @RequestHeader and method annotation @Requestmapping attribute headers  , as well as parameter attributes @RequestParam and parameter attributes @Param And  the characteristics of the method annotation @Requestmapping attribute params  .


problem analysis:

Let’s observe an example of successfully calling the Fegin interface in my project . This is an interface method getToken defined in the Fegin interface :

application.properties:

test-url=第三方服务接口ip地址和端口号

TestFegin:

@FeignClient(name = "test", url = "${test-url}")
@Component
public interface TestFegin {
	//获取用户的token
	@PostMapping(value = "/auth/oauth/token",
				headers = {"Authorization=Basic c2FiZXI6c2FiZXJfc2VjcmV0","content-type=application/x-www-form-urlencoded"})
	JSONObject getToken(@RequestHeader("Tenant-Id") String tenantId,
	                    @RequestParam(value = "grant_type", defaultValue = "password") String grantType,
	                    @RequestParam("username") String username,
	                    @RequestParam("password") String password);
}

TestController :

@CrossOrigin
@RestController
public class TestController {

    @Autowired TestFegin testFegin;

	/**
	 * 获取token
	 *
	 * @return
	 */
	@PostMapping("/getToken")
	public JSONObject getToken(@RequestBody JSONObject jsonObject) {
		JSONObject response = new JSONObject();
		if ( isLogin ) {
			response.put("status", "fail");
			response.put("msg", "登录失败!");
		}
		String tenantId = jsonObject.getString("tenantId");
		String username = jsonObject.getString("username");
		String password = jsonObject.getString("password");
    JSONObject tokenInfo = testFegin.getToken(tenantId, "password", username, password);
		response.put("msg", "成功");
		response.put("code", 200);
		response.put("data", tokenInfo);
	    return response;
    }
}

Test Results:

Some knowledge can be learned from this example:

1. In the headers property of @Requestmapping (including GetMapping/PostMapping) , you can set the request header:

Pros: This way you can automatically set the default to the interface request header information , so the call requires incoming request headers , which request headers is fixed interface when you can use this way.

 

2. @RequestHeader, used to manually pass in the request header information.

Features: You can manually pass in custom request header information to the Fegin interface.

 

3. Method annotation @Header and @Requestmapping (including GetMapping/PostMapping) headers attribute, it is said that the usage and function are almost the same, so I tested it and found that it is not working. It can be seen that the rumors are fake, and it is estimated to be too What are the disadvantages,

Because the annotated method @Header has been abandoned , and now a basic need in this way, so it is not recommended that you use the annotation , interested companion, you can go to learn more about:

	//获取用户的token
	@Headers({"Authorization=Basic c2FiZXI6c2FiZXJfc2VjcmV0","content-type=application/x-www-form-urlencoded"})
	@PostMapping(value = "/blade-auth/oauth/token")
	JSONObject getToken(@RequestHeader("Tenant-Id") String tenantId,
	                    @RequestParam(value = "grant_type", defaultValue = "password") String grantType,
	                    @RequestParam("username") String username,
	                    @RequestParam("password") String password);

 

4. @RequestParam: Used to pass in parameters to the interface . The parameter will determine the existence form of the parameter according to the attribute value of the content-type of the request header . For example, in the example, it is form data ( application/x-www-form-urlencoded ) Exist, and by default, it exists in the form that the address bar is visible . The defaultValue attribute is said to be able to set default values  such as:

http://ip:port?parameter1=value1¶meter2=value2...

If you want to use the restful style  of existence, you will use @PathVariable.

 

5.@Requestmapping  attribute params: used to limit the parameter list of the interface. Only when the conditions are met can the interface be successfully called, otherwise it will be intercepted and the interface call will fail. The test finds that there is no advantage.

	//获取用户的token
	@PostMapping(value = "/blade-auth/oauth/token",
				headers = {"Authorization=Basic c2FiZXI6c2FiZXJfc2VjcmV0","content-type=application/x-www-form-urlencoded"},
				params = {"grant_type=password"})
	JSONObject getToken(@RequestHeader("Tenant-Id") String tenantId,
	                    @RequestParam(value = "grant_type", defaultValue = "password") String grantType,
	                    @RequestParam("username") String username,
	                    @RequestParam("password") String password);

Disadvantages: The default parameter value cannot be automatically set like the headers attribute, and the parameter value still needs to be manually passed in.

 

6. @Param, this annotation is often used in the dao/mapper layer as a parameter annotation.

Features: may be a method call , the incoming scrambled parameter, but do not require the parameter list to define the order parameter passing method , often annotations on parameters having a plurality of abstract methods in the dao layer parameters.

dao layers:

    List<JSONObject> getInfoJson(@Param("tableName") String tableName, @Param("columns") String columns, @Param("where") String where);

 

Guess you like

Origin blog.csdn.net/weixin_42585386/article/details/112832774