java - Spring bean 未在 Spring REST 中初始化

标签 java spring

我的 Spring beans 没有在 Spring Java Config 中初始化,我用它来创建示例 Spring REST 应用程序(由于不需要 Web.xml,我已将其删除)。并且在调用 REST 端点/dest/types 时也会收到 404。

任何人都可以帮忙吗? 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>

调度初始化器:

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

}

配置:


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

}

最佳答案

我必须进行两项修改才能解决该问题。

  1. 由于我仅使用 Java Config,因此我删除了 Web.xml。所以我必须在 Tomcat 的 server.xml 中进行更改: <Context path="/patcyyRestApp" reloadable="true" docBase="D:\battleGround\patcyyRestApp\target\patcyyRestApp"/></Host>

  2. 我必须将 Dispatcher Intializer 中的 servlet 映射更新为: return new String[] { "/patcyy/*" };

进行这两项更改后,现在工作正常。

请查看此Tomcat without web xml

关于java - Spring bean 未在 Spring REST 中初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60312499/

相关文章:

用于在 Linux 中创建图形和图表的 Java 编程实用程序

java - 线程 "main"java.lang.NullPointerException 中出现异常(请帮助我..)

java - Spring MVC 中的 session 同步

spring - Spring中的自动配置重新初始化

java - 获取值配置服务器Spring Boot

java - 为什么 boolean 值在某些情况下可以转换为字符串,而在其他情况下则不能?

java - 安全出版

Java:数组索引越界

Spring Cloud - Zuul 无法设置超时

java - 为什么 import.sql 在 Spring Boot 中会失败?