简体   繁体   English

如何使用 Feign Client Spring Boot 从另一个服务获取数据(错误:406)

[英]How to get data from another service using Feign Client Spring Boot (ERROR: 406)

When I call another Service (User-service) from one service (API-gateway) Using Feign Client, I'm getting an Error当我使用 Feign Client 从一个服务(API 网关)调用另一个服务(用户服务)时,出现错误

There are two services有两种服务

  1. User-service用户服务
  2. API-gateway API网关

In my API-gateway在我的 API 网关中

FeignClient假客户端

@FeignClient(contextId = "user-by-email",name = "user-service")
@Service
public interface UserByEmail {
    @RequestMapping(value = "/email/{email}", consumes= MediaType.APPLICATION_JSON_VALUE)
    User findByEmail(@PathVariable("email") String email);
}

Controller控制器

@RequestMapping("/test")
public class TestController {
    @Autowired
    private UserByEmail userByEmail;

    @GetMapping(value = "/{email:.+}")
    public ResponseEntity testUser(@PathVariable("email") String username) {
        return ResponseEntity.ok(userByEmail.findByEmail(username));
    }
}

I need to call the following (User-service)我需要调用以下(用户服务)

Controller控制器

@EnableFeignClients
@RestController
public class UserController extends BaseController<User> {
    @Autowired
    private UserService userService;

    @PostConstruct
    public void binder() {
        init(this.userService);
    }

    @GetMapping(value = "/email/{email}")
    public ResponseEntity findByEmail(@PathVariable("email") String email) {
        return ResponseEntity.ok(userService.findByEmail(email));
    }
}

Repository存储库

@Override
public User findByEmail(String email) {
  Query query = new Query(Criteria.where("email").is(email).and("status").is(1));
  return mongoOperations.findOne(query, User.class);
}

Service服务

@Override
public User findByEmail(String email) {
  return userDao.findByEmail(email);
}

The Error I'm getting is ..我得到的错误是..

<Map>
    <timestamp>1583924335777</timestamp>
    <status>406</status>
    <error>Not Acceptable</error>
    <message>Could not find acceptable representation</message>
    <trace>org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation&#xd;
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:246)&#xd;

Can anyone please explain What is wrong with my code, And give your valuable solutions任何人都可以解释我的代码有什么问题,并提供您宝贵的解决方案

(Basically I need to create Security in API-gateway, in order to control the access of other services) (基本上我需要在API网关中创建Security,以控制其他服务的访问)

Try Implementing Feign client as below:尝试实现 Feign 客户端如下:

@FeignClient(name = "user-service")
public interface UserByEmail {

 @RequestMapping(method = RequestMethod.GET, value = "/email/{email}", consumes = "application/json")
 User findByEmail(@PathVariable("email") String email);

 }

also make sure the Fields passed over JSON matchers User POJO.还要确保字段通过 JSON 匹配器User POJO。

I've got the solution to my Question我有我的问题的解决方案

FeignClient假客户端

@FeignClient(contextId = "user-by-email",name = "user-service")
@Service
public interface UserByEmail {
    @RequestMapping(value = "/email/{email}", consumes= MediaType.APPLICATION_JSON_VALUE)
    User findByEmail(@RequestParam("email") String email);
}

Controller控制器

@RequestMapping("/test")
public class TestController {
    @Autowired
    private UserByEmail userByEmail;

    @GetMapping(value = "/{email}")
    public ResponseEntity testUser(@RequestParam("email") String username) {
        return ResponseEntity.ok(userByEmail.findByEmail(username));
    }
}

In User-service在用户服务中

Controller控制器

@EnableFeignClients
@RestController
public class UserController extends BaseController<User> {
    @Autowired
    private UserService userService;

    @PostConstruct
    public void binder() {
        init(this.userService);
    }

    @GetMapping(value = "/email/{email}")
    public ResponseEntity findByEmail(@RequestParam("email") String email) {
        return ResponseEntity.ok(userService.findByEmail(email));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 postman 或 feign 客户端在 Spring 引导中将值设置为 @RequestAttribute - How to set value to @RequestAttribute in Spring boot using postman or feign client Spring Boot 2 - 自动装配服务时对 Feign 客户端的依赖不满意 - Spring Boot 2 - Unsatisfied dependency on Feign client when autowired for service Feign客户端,Spring Boot应用程序和rx / Observable类未找到错误 - Feign Client, Spring Boot Application, and rx/Observable Class Not Found Error Spring Boot hatoas + swagger +feign 客户端错误应用启动 - Spring Boot hateoas + swagger +feign client error application start up 如何在 spring 引导 Java 中动态获取假客户端名称和 url - how to get feign client name and url both dynamically in spring boot Java 如何使用 Spring Feign Client 传播令牌 - How to propagate token using Spring Feign Client 如何使用 Spring Boot feign 客户端进行 Oauth2 身份验证? - How to use Spring Boot feign client for Oauth2 authentication? 如何从Spring Boot Feign客户端登录到远程Web服务 - How can I login to remote webservice from spring boot feign client 如何在Spring Boot应用程序中的Feign客户端上使用WireMock? - How to use WireMock on a Feign client in a Spring Boot application? 使用伪装客户端时,Spring服务返回的内容为空 - Spring service return content empty when using feign client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM