Announcement Announcement Module
Collapse
No announcement yet.
Spring for server side code + webservice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring for server side code + webservice

    I am working on android app that connects to server to access database from mysql database.
    I am done server side code using MVC architecture.
    need to wrap the same using spring and then publish the same using webservice.
    need help.

  • #2
    Hi, you can use Spring REST to create a Restful webservice, example:

    Spring REST

    What have you used to access database? jdbc, jpa, hibernate, ....

    Comment


    • #3
      Thanks
      to access database i have used jdbc...
      My task task to convert the MVC code into one that fix into spring framework.
      What i have done is at the server side on tomcat m running the servlet that responds to client request by calling the appropriate controller then app. service and then appr. value object...
      i just want to wrap the same under spring framework..but facing problems..
      how should i move forward ...?
      Last edited by Sayaly; Mar 19th, 2012, 08:06 AM.

      Comment


      • #4
        OK, could you give us more details of these problems (code, logs, ...)?

        This depends on the level of integration that you'd like, you can wrap only your app in a Spring Container or use spring jdbc template, spring mvc, ...

        Comment


        • #5
          can i get your mail id...
          i will attach the rar and send it to you..

          Comment


          • #6
            my Servlet file has following code to authenticate the user:
            PrintWriter out = response.getWriter();

            String type = request.getParameter("type");
            String jsonstring = request.getParameter("value");
            HttpSession session = null;
            boolean flag;

            if(type.equalsIgnoreCase("login"))
            {
            try{
            //out.print(jsonstring);
            String s=new String();
            loginValue user = new loginValue();
            Gson g=new Gson();
            user=g.fromJson(jsonstring, loginValue.class);
            user=loginService.convertfromjson(jsonstring);
            flag = loginService.login(user);
            if (flag)
            out.print(1);
            else
            out.print(0);
            }catch (Throwable theException){
            System.out.println(theException);
            }
            }

            which then calls to loginService(business logic part)file..
            loginService calls to loginDAO file(whrein i have written actual database access code)..
            public static loginValue login(loginValue bean) {

            Statement stmt = null;
            String companyid = bean.getCompanyid();
            String loginid = bean.getLoginid();
            String password = bean.getPassword();

            String searchQuery =
            "select * from login where companyid='"
            + companyid
            + "' AND loginid='"
            + loginid
            + "' AND password='"
            + password
            + "'";

            try
            {
            //connect to DB
            currentCon = ConnectionManager.getConnection();
            stmt=currentCon.createStatement();
            rs = stmt.executeQuery(searchQuery);
            boolean more = rs.next();

            // if user does not exist set the isValid variable to false
            if (!more)
            {
            //System.out.println("Sorry, you are not a registered user! Please sign up first");
            bean.setValid(false);
            }
            //if user exists set the isValid variable to true
            else if (more)
            {
            bean.setCompanyid(companyid);
            bean.setLoginid(loginid);
            bean.setPassword(password);
            bean.setValid(true);
            }
            }
            catch (Exception ex)
            {
            System.out.println("Log In failed: An Exception has occurred! " + ex);
            }
            return bean;
            loginValue file has all the getters&setters...


            This is what i done which is working fine..
            Need to wrap this server side code under spring framewrok..

            Comment


            • #7
              What i have tried is under web project i have:
              loginValue.java -- getters/setters
              loginDAO.java -- db access code
              LoginService.java -- business logic
              Again under WEB-INF i have

              web.xml
              <?xml version="1.0" encoding="UTF-8"?>
              <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
              <web-app id="WebApp">
              <display-name>Project</display-name>

              <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/bean.xml</param-value>
              </context-param>

              <listener>
              <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
              </listener>
              </web-app>

              bean.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"
              xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

              <!-- This bean is the parent ApplicationContext for the WebApplicationContexts defined in the WARs.
              The context files listed here should contain beans that are used by all WARs,
              for example Services and DAO's. -->
              <bean id="ear.context" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
              <constructor-arg index="0">
              <list>
              <value>context.xml</value>
              </list>
              </constructor-arg>
              </bean>
              </beans>

              context.xml:
              <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


              <import resource="definition.xml"/>

              </beans>

              definition.xml:
              <beans xmlns="http://www.springframework.org/schema/beans"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

              <bean id="login" class="service.LoginServiceImpl" />

              </beans>



              How do i run this project?
              when deployed on server..its giving me 404 error..
              Or my approach to problem is wrong?
              Do reply....
              Last edited by Sayaly; Mar 19th, 2012, 10:09 AM.

              Comment


              • #8
                Hi, see "Using Spring in Regular Java-Based Web Apps".

                You should use the tags to write code in the forum (see FAQ).

                Comment


                • #9
                  thanks...

                  how do i solve this error..I think it is caused due to some problem while spring configuration by tomcat itself...
                  Do let me know..

                  Error:
                  IOException parsing XML document from class path resource [C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml]; nested exception is java.io.FileNotFoundException:
                  class path resource [C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml] cannot be opened because it does not exist

                  Comment


                  • #10
                    Hi, your context.xml isn't in the classpath. Have you deployed it in a .war?

                    Comment


                    • #11
                      Yes i have deployed it in war file..

                      This is the hierarchy: /WEB-INF/
                      beans.xml
                      context.xml
                      web.xml
                      The web and beans.xml file is being accessed...why is it then giving me exception in context.xml??

                      This is my beans.xml file:
                      <bean id="ear.context" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
                      <constructor-arg index="0">
                      <list>
                      <value>/WEB-INF/context.xml</value>
                      </list>
                      </constructor-arg>
                      </bean>

                      It seems that constructor of ClassPathXmlApplicationContext cannot get context.xml file right..???
                      How do i solve this probelm??

                      Comment


                      • #12
                        Hi, do you have in your web.xml this?:

                        Code:
                        	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
                        	<context-param>
                        		<param-name>contextConfigLocation</param-name>
                        		<param-value>/WEB-INF/context.xml</param-value>
                        	</context-param>

                        Comment


                        • #13
                          xml file hierarchy unser WEB-INF folder

                          My web.xml file:
                          Code:
                          <web-app id="WebApp">
                          	<display-name>Project</display-name>
                          
                          	<context-param>
                          		<param-name>contextConfigLocation</param-name>
                          		<param-value>/WEB-INF/bean.xml</param-value>
                          	</context-param>	
                          
                          	<listener>
                          		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                          	</listener>
                          </web-app>
                          my bean.xml file:
                          Code:
                          <?xml version="1.0" encoding="UTF-8"?>
                          <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          	xsi:schemaLocation="http://www.springframework.org/schema/beans
                          						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
                          	<bean id="ear.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
                          		<constructor-arg>
                          		<value>/WEB-INF/context.xml</value>
                          		</constructor-arg>
                          	</bean>
                          </beans>
                          My context.xml file:
                          Code:
                          <bean id="login" class="Trial.Controller">
                          	</bean>
                          The Controller class contains code to update database...


                          Error:
                          Code:
                          SEVERE: Context initialization failed
                          org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ear.context' defined in ServletContext resource [/WEB-INF/bean.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WebContent/WEB-INF/acontext.xml]; nested exception is java.io.FileNotFoundException: class path resource [WebContent/WEB-INF/acontext.xml] cannot be opened because it does not exist
                          	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254)
                          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
                          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
                          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
                          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
                          	at java.security.AccessController.doPrivileged(Native Method)
                          	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
                          	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
                          	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

                          Comment


                          • #14
                            OK, instead of referencing context.xml in bean.xml, you should define it in web.xml :

                            Code:
                            <servlet>
                              <servlet-name>appServlet</servlet-name>
                              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                              <init-param>
                                <param-name>contextConfigLocation</param-name>
                                <param-value>/WEB-INF/context.xml</param-value>
                              </init-param>
                              <load-on-startup>1</load-on-startup>
                            </servlet>
                            If you'd like to see a complete example, see this.

                            Comment


                            • #15
                              hey thanks..
                              i saw the complete example and tried to map my problem...
                              i defined context.xml in web.xml file..
                              but it is still giving the same error...

                              from web.xml the bean.xml file is loaded properly i guess...
                              from the bean.xml file when passes constructor argument(context.xml) to ClassPathXmlApplicationContext its throwing error right??
                              its there in WEB-INF folder..i matched the spellling too...
                              Please help with the error..

                              Comment

                              Working...
                              X