@ExceptionHandler返回视图而不是String

时间:2017-06-01 19:32:20

标签: java spring exception-handling

我在Spring中编写了以下代码来处理自定义异常。我的异常处理程序正在执行但是它返回一个如下所示的视图而不是String。我可以知道我的代码有什么问题。提前谢谢。

enter image description here

代码:

                @Controller
                public class RestController {


                @ExceptionHandler(SpringException.class)
                    public String handleCustomException(SpringException ex) {

                        String jsonInString = "{}";
                        JSONObject json = new JSONObject();
                        Map<String,String> errorMap = new HashMap<String,String>();
                        errorMap.put("errorMessage",ex.getMessage());
                        System.out.println("Inside exception handler");
                        json.putAll(errorMap);

                        jsonInString = json.toJSONString();
                        logger.info(jsonInString);

                        return jsonInString;


                    }


                @RequestMapping(value = "/v1/dist_list/{emailId}/members", method = RequestMethod.GET)
                    public @ResponseBody String getDistributionListMember(@PathVariable String emailId) throws Exception, SpringException {

                String retStatus = null;
                retStatus = dataServices.getDistributionListMember(emailId, callerId);


                if (!retStatus.isEmpty()) {
                                if (retStatus.contains("callerid is not valid")) {



                                    throw new SpringException("Error", retStatus);



                                }



                }


                }

SpringException类

                package com.uniteid.model;

            import org.springframework.http.HttpStatus;

            public class SpringException extends RuntimeException {
                private static final long serialVersionUID = 1L;

                private String errCode;
                private String errMsg;

                public String getErrCode() {
                    return errCode;
                }

                public void setErrCode(String errCode) {
                    this.errCode = errCode;
                }

                public String getErrMsg() {
                    return errMsg;
                }

                public void setErrMsg(String errMsg) {
                    this.errMsg = errMsg;
                }

                public SpringException(String errCode, String errMsg) {
                    this.errCode = errCode;
                    this.errMsg = errMsg;
                }
            }

下面是我的Spring-config.xml文件

                <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
                xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
                xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

                <context:component-scan base-package="com.uniteid.controller" />
                <mvc:annotation-driven
                    content-negotiation-manager="contentNegociationManager" />

                <bean
                    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                    <property name="location" value="classpath:uniteidrest.properties" />
                    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
                </bean>

                <bean id="dataSource"
                    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

                    <property name="url">
                        <value>${eidms.url}</value>
                    </property>
                    <property name="username">
                        <value>${eidms.username}</value>
                    </property>
                    <property name="password">
                        <value>${eidms.password}</value>
                    </property>
                </bean>

                <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
                    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" 
                    /> <property name="url" value="jdbc:oracle:thin:@un.org:1521:EIDMSUAT" 
                    /> <property name="username" value="EIDMSUAT" /> <property name="password" 
                    value="NewPass" /> </bean> -->

                <bean id="contentNegociationManager"
                    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
                    <property name="defaultContentType" value="application/json" />
                    <property name="ignoreAcceptHeader" value="true" />
                </bean>

                <bean id="sessionFactory"
                    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                    <property name="dataSource" ref="dataSource" />
                    <property name="annotatedClasses">
                        <list>
                            <value>com.uniteid.model.User</value>
                        </list>
                    </property>
                    <property name="hibernateProperties">
                        <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                            <prop key="hibernate.connection.pool_size">10</prop>
                        </props>
                    </property>
                </bean>

                <bean
                  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix">
                        <value>/WEB-INF/pages/</value>
                    </property>
                    <property name="suffix">
                        <value>.jsp</value>
                    </property>
                </bean>

                <bean class = "org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
                  <property name = "exceptionMappings">
                     <props>
                        <prop key = "com.uniteid.model.SpringException">
                           ExceptionPage
                        </prop>
                     </props>
                  </property>
                  <property name = "defaultErrorView" value = "error"/>
               </bean>



                <bean id="txManager"
                    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
                    <property name="sessionFactory" ref="sessionFactory" />
                </bean>

                <bean id="persistenceExceptionTranslationPostProcessor"
                    class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

                <bean id="dataDao" class="com.uniteid.dao.DataDaoImpl"></bean>
                <bean id="dataServices" class="com.uniteid.services.DataServicesImpl"></bean>
            </beans>

0 个答案:

没有答案