0

I have a restful server i want to deploy on an AWS virtual machine. The application works fine on my local tomcat. I am able to access to the tomcat's default pages & manager apps from my browser and it works fine as well. But when i deploy the war file on tomcat it deploys but says

FAIL - Application at context path /zift-0.0.1-SNAPSHOT could not be started

I checked on catalina.out and realized i get mostly "UnsatisfiedDependencyExceptions", weird that i get none of them in my own computer. Here is the catalina.out:

0:10:45.466 [http-nio-8080-exec-16] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController': Unsatisfied dependency expressed through field 'projectService'; nes$
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
        at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4851)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
        at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1284)
        at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:666)
        at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:217)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:616)

And this goes on with other components with same exceptions.

Here is the pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zift.org</groupId>
    <artifactId>zift</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>ziftorganizasyon</name>
    <description>Zift Organizasyon</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
        <start-class>com.ziftorg.configurations.ZiftInitializer</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.0.12.RELEASE</version>
        </dependency>


        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>


        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>




    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

JavaConfig Files:

package com.ziftorg.configurations;


import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;


public class ZiftInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { ZiftConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }




}

And this:

package com.ziftorg.configurations;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc 
@ComponentScan(basePackages = "com.ziftorg")
public class ZiftConfiguration {


    @Bean
    public ViewResolver viewResolver() {

        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsps/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }


    @Bean
    public DataSource dataSource() {
        final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
        dsLookup.setResourceRef(true);
        DataSource dataSource = dsLookup.getDataSource("jdbc/zift");
        return dataSource;
    } 

}

One of the Controller's:

package com.ziftorg.controllers;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
//import com.mysql.jdbc.Constants;
import com.ziftorg.models.Chapter;
import com.ziftorg.models.Project;
import com.ziftorg.models.User;
import com.ziftorg.services.ProjectService;

import sun.awt.RepaintArea;

@JsonIgnoreProperties(ignoreUnknown = true)
@RestController
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    // GET METHOD TO RETRIEVE ALL PROJECTS
    @CrossOrigin(origins = com.ziftorg.Constants.CORS_ORIGIN)
    @RequestMapping(value = "/projects", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<List<Project>> getAllProject() {

        List<Project> projects = projectService.findAll();

        return new ResponseEntity<List<Project>>(projects, HttpStatus.OK);
    }

    // GET Request to retrieve a single project by ID Numberto
    @CrossOrigin(origins = com.ziftorg.Constants.CORS_ORIGIN)
    @RequestMapping(value = "/projects/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Project> getSingleProject(@PathVariable("id") long id) {

        Project project = projectService.findById(id);

        if (project == null)
            return new ResponseEntity<Project>(HttpStatus.NOT_FOUND);

        return new ResponseEntity<Project>(project, HttpStatus.OK);
    }

    // POST request to create a BLANK PROJECT and return it back
    @CrossOrigin(origins = com.ziftorg.Constants.CORS_ORIGIN)
    @RequestMapping(value = "/projects", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Project> create(@RequestBody Project project, UriComponentsBuilder ucBuilder) {

        System.out.println("passed project to POST project is : " + project);
        System.out.println("UserID wanted to create a project is: " + project.getAuthorId());

        Project p = projectService.create(project.getAuthorId());

        return new ResponseEntity<Project>(p, HttpStatus.OK);

    }

    @RequestMapping(value = "projects/{id}", method = RequestMethod.DELETE)
    @CrossOrigin(origins = com.ziftorg.Constants.CORS_ORIGIN)
    public ResponseEntity<Project> delete(@PathVariable("id") int id) {

        Project project = projectService.findById(id);
        if (project == null)
            return new ResponseEntity<Project>(HttpStatus.NOT_FOUND);

        projectService.delete(project);
        return new ResponseEntity<Project>(HttpStatus.NO_CONTENT);
    }

    @RequestMapping(value = "projects/{id}", method = RequestMethod.PUT)
    @CrossOrigin(origins = com.ziftorg.Constants.CORS_ORIGIN)
    public ResponseEntity<Project> update(@PathVariable("id") int id, @RequestBody Project project) {

        System.out.println("received project city for update is : " + project.getCity());
        System.out.println("came here too");
        Project currentProject = projectService.findById(id);

        if (currentProject == null)
            return new ResponseEntity<Project>(HttpStatus.NOT_FOUND);

        currentProject.setName(project.getName());
        currentProject.setAuthor(project.getAuthor());
        currentProject.setPartnerCountries(project.getPartnerCountries());
        currentProject.setCity(project.getCity());
        currentProject.setStartDate(project.getStartDate());
        currentProject.setEndDate(project.getEndDate());
        currentProject.setChapters(project.getChapters());

        projectService.update(currentProject);
        return new ResponseEntity<Project>(currentProject, HttpStatus.OK);
    }

    @RequestMapping(value = "projects/{id}/chapters/{chapterID}", method = RequestMethod.PUT)
    @CrossOrigin(origins = com.ziftorg.Constants.CORS_ORIGIN)
    public ResponseEntity<String> update(@PathVariable("id") long id, @PathVariable("chapterID") long chapterID,
            @RequestBody Chapter chapter) {

        Project p = projectService.findById(id);

        List<Chapter> chapters = p.getChapters();

        for (Chapter c : chapters) {

            if (c.getId() == chapterID) {
                System.out.println("will print the subtitles now");
                System.out.println(chapter.getSubtitles().toString());

                projectService.updateChapter(chapter);

            }

        }

        return new ResponseEntity<String>(HttpStatus.OK);

    }

}

And here is the Service class regarding to the previous Controller:

package com.ziftorg.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.ziftorg.models.Chapter;
import com.ziftorg.models.Project;
import com.ziftorg.models.Subtitle;
import com.ziftorg.repositories.ProjectRepository;

@Service("projectService")
@Qualifier("localProjectRepository")
public class ProjectService {

    @Autowired
    private ProjectRepository projectRepository;

    public void setProjectRepository(ProjectRepository projectRepository) {

        this.projectRepository = projectRepository;
    }

    public List<Project> findAll() {

        return projectRepository.findAll();
    }

    public Project findByName(String name) {

        return projectRepository.findByName(name);
    }

    public Project findById(long id) {

        return projectRepository.findById(id);
    }

    public void update(Project project) {
        projectRepository.update(project);
    }

    public void delete(Project project) {

        projectRepository.delete(project);
    }

    public void create(Project project) {

        projectRepository.create(project);
    }

    public Project create(long userID) {

        return projectRepository.create(userID);
    }

    public boolean projectExists(Project project) {

        return projectRepository.findById((int) project.getId()) != null;
    }

    public void updateChapter(Chapter chapter) {

        projectRepository.updateChapter(chapter);

    }

}

1 Answers1

0

When you said that you're not having problems on your computer is because you're running your project on the embedded tomcat in the STS? Because if that's the case you are have to add WebApplicationInitializer.

Carlos
  • 804
  • 1
  • 7
  • 11
  • Im not sure what this means, but i am not using STS, i have tomcat installed and as a server it is integrated in Eclipse. – Ibrahim Erol Mar 01 '17 at 02:22
  • 1
    @IbrahimErol This AWS server have access to the database? Because the problem I saw is a datasourse problem, this means that the app can't create that bean. Failed to look up JNDI DataSource with name 'jdbc/zift'. – Carlos Mar 01 '17 at 02:31
  • mysql server is also in aws's virtual machine. i set the jndi source but still doesnt help. Above, @Frank Wu shared something that might fix this problem but i mistakenly removed his comment i think. Here in this link he provided, he is talking about context class loader. But i have no idea where to put this code to be honest: http://stackoverflow.com/questions/8253958/classnotfoundexception-when-using-custom-sslsocketfactory/31690328#31690328 – Ibrahim Erol Mar 01 '17 at 02:35
  • Can you try to change this on your DataSource configuration bean `getDataSource("java:jdbc/zift");` – Carlos Mar 01 '17 at 02:54
  • Unfortunately it wouldnt even work on my own machine. – Ibrahim Erol Mar 01 '17 at 03:11
  • sorry if I'm couldn't be helpful but you can check this code https://github.com/purrox/Spring-example/blob/master/src/main/java/hello/datasource.java This is working for me in a weblogic Server. – Carlos Mar 01 '17 at 04:02