[英]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有两种服务
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
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:246)
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.