Partage
  • Partager sur Facebook
  • Partager sur Twitter

Spring Feign exception message

problème avec le cours sur les microService

    27 mai 2020 à 17:27:11

    Bonjour , j'ai un problème sur le cours : optimisez votre architecture MicroService.
    Donc le chapitre : fait communique vos service avec Feign , j'arrive bien a récupéré le type d'erreur 404 (dans la video)
    Mais quand je renvoi l'exception le message d'erreur n'est pas renvoyer , le type d'erreur ( 404 ) lui est bien renvoyer . 


    voici mon pom.xml de la parti clientui :

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.3.0.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    
    	<groupId>com.clientui</groupId>
    	<artifactId>clientui</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>client-ui</name>
    	<description>Client UI de l'application</description>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    		<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.cloud</groupId>
    			<artifactId>spring-cloud-starter-openfeign</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    			<exclusions>
    				<exclusion>
    					<groupId>org.junit.vintage</groupId>
    					<artifactId>junit-vintage-engine</artifactId>
    				</exclusion>
    			</exclusions>
    		</dependency>
    	</dependencies>
    
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>org.springframework.cloud</groupId>
    				<artifactId>spring-cloud-dependencies</artifactId>
    				<version>${spring-cloud.version}</version>
    				<type>pom</type>
    				<scope>import</scope>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    </project>
    

    toujour dans la parti clientui le controleur :

    @Controller
    public class ClientController {
    
        @Autowired
        MicroserviceProduitProxy microserviceProduitProxy;
    
        @RequestMapping("/")
        public String accueil(Model model) {
    
            List<ProductBean>  listproduct = microserviceProduitProxy.listeDesProduits();
    
    
            model.addAttribute("listproduct", listproduct);
    
            return "Accueil";
        }
    
    }

    la parti dans clientui qui ne renvoi pas le message mais bien le le bon status 

    public class CustomErrorDecoder implements ErrorDecoder {
    
        private final ErrorDecoder defaultErrorDecoder = new Default();
    
        @Override
        public Exception decode(String s, Response response) {
    
            if (response.status() == 404){
    
                System.out.println( "response body :" +response.body());;
    
                throw new ProductNotFoundException("liste des produits non trouvés");
    
            }
    
            return defaultErrorDecoder.decode(s,response);
        }
    }

    L'assoication du service ( le proxy )

    @FeignClient(name = "microservice-produits" , url = "localhost:9001")
    public interface MicroserviceProduitProxy {
    
        @GetMapping(value = "/Produits")
        List<ProductBean> listeDesProduits();
    
        @GetMapping(value = "/Produits/{id}")
        ProductBean recupererUnProduit(@PathVariable("id") int id);
    }

    dans la parti micro-service produit le pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.mproduits</groupId>
    	<artifactId>mproduits</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>mproduits</name>
    	<description>Microservice de gestion des produits</description>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.0.0.RELEASE</version>
    		<relativePath/> <!-- lookup parent from repository -->
    	</parent>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    		<java.version>1.8</java.version>
    	</properties>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-data-jpa</artifactId>
    		</dependency>
    
    
    		<dependency>
    			<groupId>com.h2database</groupId>
    			<artifactId>h2</artifactId>
    			<scope>runtime</scope>
    		</dependency>
    
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    
    
    </project>
    


    le controleur 

    @RestController
    public class ProductController {
    
        @Autowired
        ProductDao productDao;
    
        // Affiche la liste de tous les produits disponibles
        @GetMapping(value = "/Produits")
        public List<Product> listeDesProduits(){
    
            List<Product> products = productDao.findAll();
    
            if(!products.isEmpty()) throw new ProductNotFoundException("Aucun produit n'est disponible à la vente");
    
            return products;
    
        }
    
        //Récuperer un produit par son id
        @GetMapping( value = "/details-produit/{id}")
        public Optional<Product> recupererUnProduit(@PathVariable int id) {
    
            Optional<Product> product = productDao.findById(id);
    
            if(!product.isPresent())  throw new ProductNotFoundException("Le produit correspondant à l'id " + id + " n'existe pas");
    
            return product;
        }
    }


    donc je rappel ma question :

    Je recherche a recupére le message d'erreur , qui est justement attendu .

    Mais dans quand je fait :

      throw new ProductNotFoundException("liste des produits non trouvés");
    je retrouve le le status de l'erreur mais pas de nouveau message injecter.
    Je ne comprend pas est-ce que feign qui est mal ecrit (paramétre) ou c'est un problem avec le conteneur Spring 


    Si quelqu'un peut m'aider 

    • Partager sur Facebook
    • Partager sur Twitter
      22 mars 2023 à 17:24:08

      Bonjour,

      j'ai aussi le meme type d'erreur je n'arrive pas a afficher le message de Feign dans le body en cas d'erreur

      • Partager sur Facebook
      • Partager sur Twitter

      Spring Feign exception message

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown