简体   繁体   中英

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

There are two services

  1. User-service
  2. API-gateway

In my API-gateway

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)

Try Implementing Feign client as below:

@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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM