Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Feign remotely calls methods that pass object parameters and return custom paging data

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the method of passing object parameters and returning custom paging data by Feign remote call". In daily operation, it is believed that many people have doubts about the method of passing object parameters and returning custom paging data by Feign remote call. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "the method of passing object parameters and returning custom paging data by Feign remote call"! Next, please follow the editor to study!

Feign introduction

Feign is an open source lightweight rest client of Netflix, and it is very convenient to implement Http client using Feign. Spring Cloud introduces Feign and integrates Ribbon to implement client-side load balancing calls.

Feign test 1. Added configuration information feign: httpclient: enabled: true2 to the yml file. Dependency (consumer side) org.apache.httpcomponents httpclient io.github.openfeign feign-httpclient 10.1.0 introduced in the client pom.xml file

Note here

It depends here on why you use io.github.openfeign 's httpclient instead of replacing Feign's native httpclient with Apache's HttpClient.

Read a lot of articles, all about quoting this dependency:

But I don't know what the problem is, and I always report an error when getting the return result:

Caused by: java.lang.NoSuchMethodError: feign.Response.create (ILjava/lang/String;Ljava/util/Map;Lfeign/Response$Body;) Lfeign/Response

Looking at the source code, we can see that when openfeign accepts the return value, it calls not the code of httpclient's feign-core package but the code of its own feign-core, while the Response class in its own feign-core package does not have a create method. Due to the inconsistency of the Retryer interface in the two feign-core packages, the feign-core version of openfeign is 10.1.0 httpclient and the version 8.16.1.

After looking for a problem for a long time, I finally replaced the dependency of httpclient with the dependency of OK in the code block.

3. The service invocation side interface is

The POST request is used here, and step 6 is explained.

@ Slf4j@RequestMapping ("/ list") @ RestControllerpublic class WebQueryListController {@ Autowired private TourismListService listService; @ PostMapping ("/ ad/allByQuery") public ApiResult allByQuery (@ RequestBody TourismAdQuery adQuery) {ApiResult pageApiResult = listService.selectAllAdByQuery (adQuery); return pageApiResult;}

My TourismAdQuery class inherits the Page class (seems to have no effect)

@ Datapublic class TourismAdQuery extends Page {/ * title * / private String title;. } 4. Service caller Service code

The @ PostMapping address here is the api interface address provided by the server.

@ FeignClient (name = "fisher-back-service", fallback = TourismListFallback.class, configuration = FeignConfig.class) public interface TourismListService {/ * paged query ads according to query conditions * @ param adQuery * @ return * / @ PostMapping (value = "/ ad/get/allByQuery") ApiResult selectAllAdByQuery (TourismAdQuery adQuery); 5. The Fallback of the service caller is @ Slf4j@Servicepublic class TourismListFallback implements TourismListService {/ * paged query advertisement according to query condition * * @ param adQuery * @ return * / @ Override public ApiResult selectAllAdByQuery (TourismAdQuery adQuery) {log.error ("call selectAllAdByQuery method exception, parameter: {}", adQuery); return null;} 6. The service provider code is

The parameter passed here is a POJO class, and the parameter cannot be received when feign is called remotely without the @ RequestBody annotation.

Although the Get request method is mostly used when getting data, the GET method cannot receive the @ RequestBody parameter body.

So I had to change the GET request to the POST request.

@ RestController@RequestMapping ("/ ad") public class TourismAdController extends BaseController {@ Autowired private TourismAdService adService; @ ApiOperation (value = "paging query advertisement based on query condition", notes = "pagination query advertisement based on query condition", httpMethod = "POST") @ PostMapping ("/ get/allByQuery") public ApiResult allByQuery (@ RequestBody TourismAdQuery adQuery) {return adService.selectAllByQuery (adQuery);} 7. test

You can call API http://localhost:9009/list/ad/allByQuery to pass parameters in json format:

{"address": "", "title": "Advertising space 1", "size": 6}

Successfully paging to get the custom return type data:

{"data": {"records": [{"id": 1, "title": "Advertising Space 1", "description": "Investment Promotion", "sort": 0, "datetime": "2019-09-26 17:46:50", "updatetime": "2019-09-26 17:46:50", "peopleid": 0 "display": 0, "content": "04004", "file": "444//44.jpg", "leaseperson": "find out", "address": "Hangzhou", "idcard": "1154465656656", "phone": "131654799"}], "total": 1, "size": 6, "current": 1 "searchCount": true, "pages": 1}, "code": 200,200, "message": "paging obtained successfully"} Feign calls paging API to report error: Method has too many Body parameters

Interface definition:

@ ApiOperation (value = "paging query session") @ PostMapping (Routes.SESSIONS_QUERY) JsonResult querySessions (@ RequestBody @ Valid SessionsQo qo,@PageableDefault (size = 20, sort = "id", direction = Sort.Direction.DESC) Pageable pageable)

The service consumer calls and reports an error:

Method has too many Body parameters: public abstract com.xingren.common.data.JsonResult com.xingren.xxx.yyy.contract.api.controller.ISessionController.querySessions (com.xingren.xxx.yyy.contract.qo.SessionsQo,org.springframework.data.domain.Pageable)

Solution method

Through search and research, there are three solutions:

1. Pass the paging attribute directly through the input parameters. The API is defined as follows:

@ ApiOperation (value = "paging query session") @ PostMapping (Routes.SESSIONS_QUERY) JsonResult querySessions (@ RequestBody @ Valid SessionsQo qo,@RequestParam ("page") Integer page, @ RequestParam ("size") Integer size, @ RequestParam ("sort") Sort sort)

2. Redundant paging objects in Qo (implemented by inheritance):

@ Data@NoArgsConstructor@ApiModel (value = "query session") public class SessionsQo extends PageableParam {@ ApiParam (value = "session id list") private List sessionIdIn = Lists.newArrayList ();.}

3. Pass it through comments (see: Issue):

Service provider definition Notes:

@ Target (ElementType.PARAMETER) @ Retention (RetentionPolicy.RUNTIME) public @ interface PageableParam {}

The service provider defines the interface:

@ ApiOperation (value = "paging query session") @ PostMapping (Routes.SESSIONS_QUERY) JsonResult querySessions (@ RequestBody @ Valid SessionsQo qo,@PageableParam @ SpringQueryMap Pageable pageable)

The service consumer defines the processor:

@ Beanpublic PageableParamProcessor pageableParamProcessor () {return new PageableParamProcessor ();} public static class PageableParamProcessor implements AnnotatedParameterProcessor {private static final Class ANNOTATION = PageableParam.class; @ Override public Class

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report