0

I'm trying to get data from a server given a id every thing is working well, so i want to handle the exceptions in my code like invalid id such that for that id there is no data at server the server throws me some error so i want to handle that exception so i tried to use web exception but spring is not detecting it

error is:

WebException cannot be resolved to a type

my pom file:

<properties>
        <spring.version>4.1.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>


        <dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>


    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.1.0.RELEASE</version>
</dependency>



    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>



        <!-- Spring AOP dependency -->
        <dependency>
                <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>

    <!-- Hibernate framework -->
        <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.3.ga</version>
</dependency>

    <!--<dependency>
        <groupId>hibernate</groupId>
        <artifactId>hibernate3</artifactId>
        <version>3.2.3.GA</version>
    </dependency> -->


    <!-- Hibernate library dependecy start -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
    </dependency>
    <!-- Hibernate library dependecy end -->

 <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>3.6.0.Final</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.6.5</version>
  </dependency>

<dependency>
    <groupId>javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.12.1.GA</version>
</dependency>
<dependency>

    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>


 <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.11</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

<dependency>
    <groupId>org.gvnix</groupId>
    <artifactId>org.gvnix.web.exception.handler.roo.addon.annotations</artifactId>
    <version>2.0.0.M1</version>
</dependency>




    </dependencies>

my java file:

try{
          result = restTemplate.exchange("https://example.com/{id}",  HttpMethod.GET, new HttpEntity<String>(createHeaders()), String.class);

        }

        catch(WebException e) {
            System.out.println(e);

        }
Labeo
  • 5,831
  • 13
  • 47
  • 77
  • 1
    Is `restTemplate` the Spring [RestTemplate](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-org.springframework.http.RequestEntity-java.lang.Class-)? If it is, the exchange method doesn't throw a WebExceptoin, just a RestClientException, so you can't have it in a try-catch that's catching a WebException because it'll never happen. – Roddy of the Frozen Peas Aug 31 '15 at 04:28
  • yes it is Spring RestTemplate – Labeo Aug 31 '15 at 04:31
  • to accept webexception in rest template what method do i need to use? – Labeo Aug 31 '15 at 04:37
  • 1
    You don't. None of the methods in RestTemplate throw a WebException. You can't catch an exception that will never be thrown. Catch the RestClientException instead. – Roddy of the Frozen Peas Aug 31 '15 at 05:19

0 Answers0