为什么会发生此错误java.lang.IllegalStateException?

时间:2019-10-06 08:16:12

标签: java json spring-mvc postman

@RequestMapping(value = "/download", method = RequestMethod.GET, produces ="application/json" , consumes="application/json")
public String downloadProjectRequirementdetails(@RequestParam("StudID") int studID) throws JSONException, JsonProcessingException {     
        Map<String, Object> response = new HashMap<String, Object>();
        Map<String, Object> result = new HashMap<String, Object>();

        List<Student> list = new ArrayList<Student>();
    /*      int stat = (int) result.get("o_status");
        if( stat ==106)
                 {
        */
        result = studDao.downloadtestScnario(studID);
        json = mapper.writeValueAsString(result);   
        JSONObject jsonObj = new JSONObject(json);
        if (jsonObj.has("#result-set-1")) {
            JSONArray jsonArray = jsonObj.getJSONArray("#result-set-1");
            if (jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject MainObj = jsonArray.getJSONObject(i);
                    Student testdetails = new Student();
                    testdetails.setStudID(MainObj.getInt("StudID"));
                    testdetails.setFirstName(MainObj.getString("FirstName"));
                    testdetails.setLastName(MainObj.getString("LastName"));
                    testdetails.setAddress(MainObj.getString("Address"));
                    testdetails.setMobileNo(MainObj.getInt("MobileNo"));
                    list.add(testdetails);

                }
                response.put("response", list);
                response.put("StatusCode", 200);
                response.put("Message", "Download Completed");
            }

        } else {
            response.put("StatusCode", 204);
            response.put("Message", "No data found!!!");
        }
                 //}

        json = mapper.writeValueAsString(response);
        return json;
        }   

public Map<String, Object> downloadtestScnario(int studID) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            SqlParameterSource in = new MapSqlParameterSource()
             .addValue("i_Stud_id", studID);

            map = sp_DownloadStudDetails.execute(in);


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }
        return map;
    }

存储过程为

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_DownloadStudDetails`(
in i_StudID int(11),
out o_status int(11)
)
BEGIN

#select * from student;
select StudID,FirstName,LastName,MobileNo,Address from student where StudID = i_StudID;
set o_status =106;
END

当我打http://localhost:8080/TestSpringProject/download?StudID=1时  在邮递员中,我收到此错误:

<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> &#47;TestSpringProject&#47;download</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.26</h3></body></html>

和eclipse中的相​​同URL,我给出以下错误:

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
        at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:177)
        at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1035)
        at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1011)
        at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:961)
        at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:583)
        at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:116)
        at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4730)
        at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5396)
        at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:187)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:841)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1384)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1374)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:909)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:421)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:932)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:633)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:344)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:475)

2 个答案:

答案 0 :(得分:1)

该错误表明您没有创建bean。创建一个xml bean配置文件,然后为该控制器创建downloadProjectRequirementdetails方法所在的bean!您可以使用以下帖子作为指导。

https://medium.com/omarelgabrys-blog/spring-a-head-start-beans-configuration-part-2-4a8c239b070a

答案 1 :(得分:0)

此异常是由于您正在提供侦听器ContextLoaderListener

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

,但是您没有为spring配置文件提供context-param。像applicationContext.xml一样,您必须在下面的代码段中进行配置

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>applicationContext.xml</param-value>
</context-param>

如果您提供的是基于Java的spring配置,则意味着您当时不使用xml文件进行spring配置,您必须提供以下代码:

<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
    <param-name>contextClass</param-name>
    <param-value>
    org.springframework.web.context.support.AnnotationConfigWebApplicationContext
    </param-value>
</context-param>

<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also
be specified for component-scanning -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.nirav.modi.config.SpringAppConfig</param-value>
</context-param>