Ganesh Pabale
Ganesh Pabale

Reputation: 55

why this error occure java.lang.IllegalStateException?

@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;
        }   

and

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

and

stored procedure is

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

When I hit http://localhost:8080/TestSpringProject/download?StudID=1 in postman I get this error:

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

and the same URL in eclipse I gives the following error:

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)

Upvotes: 0

Views: 1182

Answers (2)

TanvirChowdhury
TanvirChowdhury

Reputation: 2445

This exception come due bean problem .you have to Create an xml bean configuration file and then create the bean for this controller.

Also few other things you may note : you are providing listener ContextLoaderListener

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

but you are not providing context-param for your spring configuration file. like applicationContext.xml You must provide below snippet for your configuration

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

If You are providing the java based spring configuration , means you are not using xml file for spring configuration at that time you must provide below code:

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

Upvotes: 0

Md. Shohag Mia
Md. Shohag Mia

Reputation: 374

The error states that you are not creating beans. Create an xml bean configuration file and then create the bean for this controller where downloadProjectRequirementdetails method resides! You can use the following post as a guide.

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

Upvotes: 1

Related Questions