自动装配位于JAR中的Feign界面

时间:2017-09-30 19:50:12

标签: java spring spring-cloud feign

我有一个AttachmentApi服务类,它使用位于feign interface文件中的JAR,我在client microservice中使用它作为依赖项。
该客户端微服务的controller使用AttachmentApi服务从db获取数据 当我访问该控制器的某个端点时,AttachmentApi服务是  成功自动装配,但假装界面不是,它给了我以下例外:

java.lang.NullPointerException: null
at ma.co.omnidata.framework.services.attachment.api.AttachmentApi.upload(AttachmentApi.java:26) ~[classes/:na]
at ma.co.omnidata.attachment.user.service.impl.AttachmentsUserService.uploadFile(AttachmentsUserService.java:21) ~[classes/:na]
at ma.co.omnidata.attachment.user.controllers.AttachmentUserController.uploadFile(AttachmentUserController.java:31) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_144]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_144]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_144]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_144]

null是由于没有自动装配的假装接口!

知道我在springboot类中使用了以下注释

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"my.packages.including.the.feign.interface.package"})

任何建议都将不胜感激 谢谢!

更新1

Feign接口(位于JAR依赖项中)

@FeignClient(name="attachment-service", fallback=AttachmentHystrixFallback.class)
public interface AttachmentFeignClient {

@RequestMapping("attachment/{id}")
byte[] getAttachment(@PathVariable(name="id") String id);

@RequestMapping("attachments")
List<AttachmentDto> getAttachments(@RequestParam(name="attachableId") Long attachableId, 
        @RequestParam(name="className") String className, @RequestParam(name="appName") String appName);

@RequestMapping("upload")
void upload(MultipartFile file, @RequestParam(name="attachableId") Long attachableId, 
        @RequestParam(name="className") String className, @RequestParam(name="appName") String appName);

@RequestMapping("test")
String test();

}

AttachmentApi(也位于JAR依赖项中)

@Service
public class AttachmentApi implements IAttachmentApi {

@Autowired
AttachmentFeignClient client;

@Override
public byte[] getAttachment(String id) {
    return client.getAttachment(id);
}

@Override
public void upload(IAttachable entity, MultipartFile file) {
    client.upload(file, entity.getAttachableId(), entity.getClassName(), entity.getAppName());
}

@Override
public List<AttachmentDto> getAttachments(IAttachable entity) {
    return client.getAttachments(entity.getAttachableId(), entity.getClassName(), entity.getAppName());
}


public String test() {
    return client.test();
}

}

注意:我的客户端微服务无法自动装配位于JAR依赖项中的任何bean(Feign接口和AttachmentApi类)!

1 个答案:

答案 0 :(得分:0)

试试这个

@EnableFeignClients(clients = {AttachmentFeignClient.class})