简体   繁体   English

Java中带有Spring Microservices的HttpClientErrorException

[英]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.

相关问题 Spring 测试 - 未找到 HttpClientErrorException - Spring Test - HttpClientErrorException Not Found HttpClientErrorException 400 null在MicroServices中使用RestTemplate - HttpClientErrorException 400 null using RestTemplate in microServices Spring - Java项目中的微服务和docker - Spring - microservices and dockers in Java project 春季启动-使用@ControllerAdvice处理HttpClientErrorException - SPRING BOOT - Handle HttpClientErrorException using @ControllerAdvice Spring Social Facebook-HttpClientErrorException:400 OK - Spring Social Facebook - HttpClientErrorException: 400 OK Spring HttpClientErrorException 和 RestClientException 获取请求 URL - Spring HttpClientErrorException and RestClientException getting request URL Spring HttpClientErrorException没有提供响应主体的详细信息 - Spring HttpClientErrorException provides no details from response body 在Spring中为RestTemplate客户端抛出ResourceAccessException和HttpClientErrorException - Throwing ResourceAccessException vs HttpClientErrorException for RestTemplate client in Spring Java Spring - 如何保持 2 个数据库/2 个微服务一致 state - Java Spring - how to keep 2 databases/2 microservices in consistent state 如何使用Java Spring同步在码头集群中运行的微服务实例 - How to synchronize microservices instances running in a jetty cluster using Java Spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM