尝试重新模板化getforobject时出现404错误

时间:2019-10-13 14:30:20

标签: java spring-boot resttemplate

我有与产品服务一起使用的目录服务,以获取数据(微服务)。当我尝试在目录服务中使用getForObject时,出现错误404。

    @RestController
    @RequestMapping("/catalog")
    public class ProductCatalogApi {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("")
    public String hello(){
        return "Heelloooo";
    }

    @GetMapping("/{category}")
    public void getProductsByCategoryName(@PathVariable String category) {
        UserProduct  userProduct = restTemplate.getForObject(
                "http://shop-product-service/shop/products" + category,
                UserProduct.class);
        System.out.println("dsdasa--------"+ userProduct);
    }

这是我的产品服务:

    @RestController
    @RequestMapping("/shop")
    public class ProductController {
    @Autowired
    ProductRepository productRepository;

    @GetMapping("/all")
    public List<Product> index(){
        return productRepository.findAll();
    }

    @GetMapping("/product/{id_product}")
    public Optional<Product> showByProductId(@PathVariable String id_product){
        return productRepository.findById(id_product);
    }

    @GetMapping("/products/{category}")
    public List<Product> showByCategoryName(@PathVariable String category){
        return productRepository.findByCategory(category);
    }

    }

因此,当我尝试建立链接:http://localhost:8082/catalog/electronics时,出现错误,请帮助我。

1 个答案:

答案 0 :(得分:2)

您在类ProductCatalogApi中丢失了字符“ /”:

restTemplate.getForObject(“ http://shop-product-service/shop/products” +类别,                 UserProduct.class);

http://shop-product-service/shop/products => http://shop-product-service/shop/products/

相关问题