feign的使用注意
2021-04-22 本文已影响0人
尘埃里的玄
首先feign接口处和被调用的controller接口无法相同。
@FeignClient(name = ModuleConstant.ModuleName.API)
public interface ReportFileRemote {
@GetMapping("/api/reportRemote/allAmByArea")
@ApiOperation(value = "查询全校的区域的工作时段和非工作时段")
List<Map<String, Object>> allAmByArea(@RequestParam("dateStart") String dateStart,@RequestParam("dateEnd") String dateEnd);
}
@Slf4j
@RestController
@RequestMapping("/api/reportRemote")
@Api("远程调用接口")
public class ReportFileRemoteController {
@Autowired
private AmMeterDataService amMeterDataService;
@Autowired
private PartmentService partmentService;
@Autowired
private ArchitectureService architectureService;
@Autowired
private DictionaryValueService dictionaryValueService;
@Autowired
private WaterMeterDataService waterMeterDataService;
@GetMapping("/allAmByArea")
@ApiOperation(value = "查询全校的区域的工作时段和非工作时段")
public List<Map<String, Object>> allAmByArea( @RequestParam("dateStart") String dateStart, @RequestParam("dateEnd") String dateEnd) {
String[] start = dateStart.split("-");
DateVo startDate = new DateVo(Integer.valueOf(start[0]), Integer.valueOf(start[1]), Integer.valueOf(start[2]));
String[] end = dateEnd.split("-");
DateVo endDate = new DateVo(Integer.valueOf(end[0]), Integer.valueOf(end[1]), Integer.valueOf(end[2]));
List<Map<String, Object>> maps = amMeterDataService.allAreaGross1(null, null, null, startDate, endDate, null, null, null);
return maps;
}
}
1:如果controller的接口有@RequestParm制定了参数名称,那么feign那里也要加入这个注解的属性,并且名字也要取的相同。
2:feign处必须有@RequestParm注解(针对get请求),controller处不一定需要该注解
3:如果想使用get请求传输pojo对象,就使用@SpringQueryMap注解,这个注解会帮我们解析传入的对象的参数然后拼接到url(这里不懂等会讲原理)
4:@SpringQueryMap只能使用一次,使用多次,后面参数接收的值都是填充第一个参数的值。


传入的值为4月1号和4月22号,

实际debug发现接收的都是4.1号
5:pojo对象为简单对象,里面属性不能含有对象。
我测试过,封装一个对象
@Data
public calss DatesVo{
private DateVo start;
private DateVo end;
}
@Data
public class DateVo{
private Integer year;
private Integer month;
private Integer date;
}
然后尝试将传入的参数改为@SpringQueryMap DatesVo datesVo,并不能解析复杂对象,因为feign的本质是对httpClientTemplate对象的封装,所以我们的url很难拼接一个复杂对象上去,或者无法解析复杂对象。
6:由于get的参数大小有限制,所以当传输的对象复杂时,推荐使用@RequestBody和post请求解决。
package com.sf.energy.nplatform.feign.module.datacenter;
import com.alibaba.fastjson.JSONObject;
import com.sf.energy.nplatform.common.constant.ModuleConstant;
import com.sf.energy.nplatform.common.entity.BaseMeterDataEntity;
import com.sf.energy.nplatform.common.entity.Result;
import com.sf.energy.nplatform.common.entity.StatDataEntity;
import com.sf.energy.nplatform.common.entity.query.MeterDataQueryEntity;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Date;
import java.util.List;
@FeignClient(name = ModuleConstant.ModuleName.DATACENTER)
public interface MeterDataServiceRemote {
@GetMapping(value = "/datacenter/data/findByCondition")
Result<List<JSONObject>> findByCondition(@SpringQueryMap MeterDataQueryEntity baseQueryEntity);
@PostMapping(value = "/datacenter/data/save")
Result save(@RequestBody BaseMeterDataEntity data);
@PostMapping(value = "/datacenter/data/saveBatch")
Result saveBatch(@RequestBody List<BaseMeterDataEntity> dataList);
@PostMapping(value = "/datacenter/data/deleteData")
Result deleteData(@RequestParam Long meterId, @RequestParam Integer meterType, @RequestParam Date startTime, @RequestParam Date endTime);
@GetMapping(value = "/datacenter/data/getOneAtTime")
Result<JSONObject> getOneAtTime(@RequestParam Long meterId, @RequestParam Date time, @RequestParam Integer meterType);
@GetMapping(value = "/datacenter/data/getOneBeforeTime")
Result<JSONObject> getOneBeforeTime(@RequestParam Long meterId, @RequestParam Date time, @RequestParam Integer meterType);
@GetMapping(value = "/datacenter/data/getOneAfterTime")
Result<JSONObject> getOneAfterTime(@RequestParam Long meterId, @RequestParam Date time, @RequestParam Integer meterType);
@GetMapping(value = "/datacenter/data/getOneBetweenTime")
Result<JSONObject> getOneBetweenTime(@RequestParam Long meterId, @RequestParam Integer meterType, @RequestParam Date startTime, @RequestParam Date endTime, @RequestParam String orderDirection);
@GetMapping(value = "/datacenter/data/getTwoBeforeTime")
Result<List<JSONObject>> getTwoBeforeTime(@RequestParam Long meterId, @RequestParam Date time, @RequestParam Integer meterType);
}