【问题标题】:why this error occure java.lang.IllegalStateException?为什么会出现这个错误 java.lang.IllegalStateException?
【发布时间】:2020-02-03 21:59:27
【问题描述】:
@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)

【问题讨论】:

    标签: java json spring-mvc postman


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      这个异常是由于 bean 问题造成的。你必须创建一个 xml bean 配置文件,然后为这个控制器创建 bean。

      还有一些您可能会注意到的其他事项: 您正在提供侦听器 ContextLoaderListener

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

      但您没有为您的 spring 配置文件提供上下文参数。像 applicationContext.xml 你必须为你的配置提供下面的 sn-p

      <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>
      

      【讨论】:

        猜你喜欢
        • 2015-01-04
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 2013-01-29
        相关资源
        最近更新 更多