简体   繁体   中英

Spring beans are not initializing in Spring REST

My Spring beans are not getting initialized in Spring Java Config which I am using to create a sample Spring REST Application(As No Web.xml is required I have deleted it) . And also getting 404 while calling the REST endpoint /dest/types.

Can anyone please help. Project Structure

Pom.xml :

<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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.travel</groupId>
    <artifactId>patcyyRestApp</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>patcyyRestApp Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <springframework.version>5.0.9.RELEASE</springframework.version>
        <jackson.library>2.9.6</jackson.library>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <hibernate.core.version>5.3.6.Final</hibernate.core.version>
        <javax.servlet.api.version>3.1.0</javax.servlet.api.version>
        <lombok.version>1.18.12</lombok.version>
        <apache.commons.version>3.9</apache.commons.version>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${javax.servlet.api.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.library}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.core.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${apache.commons.version}</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>patcyyRestApp</finalName>
    </build>
</project>

Dispatch Initializer :

package com.patcyy.rest.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class PatcyyDispatcherServletInitializer  extends 
    AbstractAnnotationConfigDispatcherServletInitializer {

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

    @Override
    protected Class<?>[] getServletConfigClasses() {
        System.out.println("Inside getServletConfigClasses");
        return new Class[] { PatcyyConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        System.out.println("Inside mapping");

        return new String[] { "/patcyy" };
    }

}

Config :


package com.patcyy.rest.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan("com.patcyy.rest")
public class PatcyyConfig {

}

Controller :

package com.patcyy.rest.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;   
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.patcyy.rest.response.DestinationTypes;
import com.patcyy.rest.service.IDestinationService;

@RestController
@RequestMapping(path = "/dest", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = 
     MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DestinationController extends BaseController {

    private final IDestinationService iDestinationService;

    /**
     * @param iDestinationService
     */
    public DestinationController(@Autowired IDestinationService iDestinationService) {
        super();
        this.iDestinationService = iDestinationService;
    }

    @GetMapping("/types")
    public ResponseEntity<List<DestinationTypes>> getDestinationTypes() {
        List<DestinationTypes> destTypes = iDestinationService.getDestinationTypes();
        return new ResponseEntity<List<DestinationTypes>>(destTypes, HttpStatus.OK);
    }

}

I had to do two modifications to resolve the issue.

  1. As i am using Java Config only, I had deleted the Web.xml. So i had to make changes in server.xml for Tomcat as : <Context path="/patcyyRestApp" reloadable="true" docBase="D:\\battleGround\\patcyyRestApp\\target\\patcyyRestApp"/></Host>

  2. I had to update the servlet mapping in the Dispatcher Intializer as : return new String[] { "/patcyy/*" };

After making this two changes, It is working fine now.

Please take a look into this Tomcat without web xml

Please share console log for further investigation. As per my understanding after seeing above code , you can replace

"/patcyy" with only "/" in getServletMappings() method.

your requested resource url will be like:

http://{hostname:port}/patcyyRestApp/dest/types

It's occurring due to invalid url servlet mapping.

i think the problem is in ComponentScan(). @ComponentScan annotation along with @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages. in your case you need to add basepackages="com.patcyy.rest"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM