Как получить данные из другого сервиса с помощью Feign Client Spring Boot (ОШИБКА: 406) - PullRequest
0 голосов
/ 11 марта 2020

Когда я вызываю другую службу (User-service) из одной службы (API-шлюз), используя Feign Client, я получаю сообщение об ошибке

Есть две службы

  1. Пользовательский сервис
  2. API-шлюз

В моем 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);
}

Контроллер

@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));
    }
}

Мне нужно позвонить по следующему (Служба поддержки)

Контроллер

@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));
    }
}

Репозиторий

@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);
}

Сервис

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

Ошибка, которую я получаю, это ..

<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;

Может кто-нибудь объяснить, пожалуйста, что не так с моим кодом, и дать ваши ценные решения

(в основном мне нужно создать Security в API-шлюзе, чтобы контролировать доступ других сервисов)

Ответы [ 2 ]

0 голосов
/ 12 марта 2020

У меня есть решение моего вопроса

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);
}

Контроллер

@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));
    }
}

В пользовательском сервисе

Контроллер

@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));
    }
}

0 голосов
/ 11 марта 2020

Попробуйте внедрить клиент Feign, как показано ниже:

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

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

 }

также убедитесь, что поля пропущены через JSON соответствий User POJO.

...