[英]HttpClientErrorException with spring microservices in java
I am trying to create 2 microservices. 我正在尝试创建2个微服务。 1st calls 2nd (add method should only check content of review and set approved flag) for approving but I am still receiving an exception
org.springframework.web.client.HttpClientErrorException: 405 null
. 第一个调用第二个(添加方法应仅检查审阅内容并设置已批准标志)以进行批准,但我仍收到异常
org.springframework.web.client.HttpClientErrorException: 405 null
。
Here you are my REST from 1st microservice 这是您来自第一微服务的REST
@RequestMapping(value = "/addreview", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> createReviewForMovie(@RequestBody Review review) {
ResponseEntity<Boolean> response = new RestTemplate().getForEntity("http://localhost:9100/add",
Boolean.class, review);
Boolean resultReview = response.getBody();
return new ResponseEntity<Boolean>(resultReview, HttpStatus.OK);
}
And the second one: 第二个:
@RequestMapping(value = "/add", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> add(@RequestBody Review review){
if(review.getReviewContent().length()<10){
review.setApproved(false);
return new ResponseEntity<Boolean>(review.isApproved(), HttpStatus.OK);
}
review.setApproved(true);
return new ResponseEntity<Boolean>(review.isApproved(), HttpStatus.OK);
}
What am I doing wrong? 我究竟做错了什么? How to call 2nd microservice from 1st?
如何从1st调用2nd microservice?
The problem is that I am using same model class ( Review.class
) with different microservices and this not allow me to execute request. 问题是我在不同的微服务中使用了相同的模型类(
Review.class
),这不允许我执行请求。
Add method could not recognize review
because I pass it from another project. 添加方法无法识别
review
因为我是从另一个项目传递过来的。
After my recode it looks like that: 重新编码后,它看起来像这样:
@RequestMapping(value = "/addreview", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> createReviewForMovie(@RequestBody Review review) {
Map<String, String> uriVariables = new HashMap<String, String>();
uriVariables.put("reviewContent", review.getReviewContent());
uriVariables.put("userName", review.getUserName());
ResponseEntity<Boolean> response = new RestTemplate().getForEntity("http://localhost:9100/add/{reviewContent}/{userName}",
Boolean.class, uriVariables);
Boolean resultReview = response.getBody();
return new ResponseEntity<Boolean>(resultReview, HttpStatus.OK);
}
@RequestMapping(value = "/add/{reviewContent}/{userName}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> add(@PathVariable("reviewContent") String reviewContent, @PathVariable("userName") String userName){
if(reviewContent.length()<10){
return new ResponseEntity<Boolean>(false, HttpStatus.OK);
}else if(userName.length()>15){
return new ResponseEntity<Boolean>(false, HttpStatus.OK);
}
return new ResponseEntity<Boolean>(true, HttpStatus.OK);
}
However I am still curious if there is any other way to use Review.class
. 但是我仍然很好奇是否还有其他方法可以使用
Review.class
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.