【问题标题】:Why is my spring boot controller not auto-wiring the Validator?为什么我的 Spring Boot 控制器没有自动连接 Validator?
【发布时间】:2017-09-17 21:20:09
【问题描述】:

我有一个具有以下功能的控制器:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.Validator;

@RestController
@RequestMapping("/")
public class MyController {

    @Autowired
    private Validator validator;

    //..elided...

    @GetMapping
    public String getSomething(@Valid @RequestBody MyRequest) {

        //This is null
        if ( validator == null ) {
            throw new Exception("is null");
        }
    }
}

我的配置类:

package com.app.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@Configuration
@EnableWebMvc
@ComponentScan("com.app")
public class ValidationConfig {

    @Bean
    public Validator validator() {

        return new LocalValidatorFactoryBean();
    }

    /* //Including this doesn't help
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setValidator(validator());
        return methodValidationPostProcessor;
    }*/
}

分级:

buildscript {
    ext {
        springBootVersion = "1.5.6.RELEASE"
    }
        repositories {
        mavenCentral()
    }

    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "org.springframework.boot"
apply plugin: "idea"
apply plugin: "jacoco"

jar {
    baseName = "my-service"
    version = "0.0.1-SNAPSHOT"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {

    //Spring Boot
    compile("org.springframework.boot:spring-boot-starter")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-actuator")

    //Testing
    testCompile("org.hamcrest:hamcrest-all:1.3")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

无论我做什么,我的控制器中的验证器都是空的!

【问题讨论】:

  • 什么是引导这段代码?看到这可能会提供进一步的见解。
  • @nicholas.hauschild 你什么意思?我用@RunWith(SpringRunner.class) @SpringBootTest public class TestMyController { 运行它。
  • @nicholas.hauschild 谢谢!你帮我发现了问题。我使用的是@InjectMocks private MyController controller,而其中的@Autowired 字段为空。更改为@Autowired @InjectMocks private MyController controller 已修复!如果您将此添加为答案(连同您的评论),我会将其标记为已回答
  • 我的问题是关于什么“开始”?看起来这是基于您上一条评论的 JUnit 测试,但我不清楚您的 ApplicationContext 的配置是什么。我对@SpringBootTest 不熟悉,所以可能会这样做......
  • 呃,我建议问题中没有足够的信息来回答它......此外,我并没有真正给出答案......你做到了!我认为您可以使用您的测试类代码编辑您的问题并自己提供答案(这在 StackOverflow 中是完全可以接受的)

标签: java spring spring-mvc spring-boot spring-validator


【解决方案1】:

在您的控制器中,您期望的是javax.validation.Validator,但在配置中您使用的是org.springframework.validation.Validator

【讨论】:

  • 即使在修复之后它仍然为空。 (我都做了春季版)
  • 您的控制器顶部没有“包”语句,代码甚至无法编译,因为您在没有声明的情况下抛出异常。你能粘贴完整的真实代码,包括主类吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
  • 2018-10-13
  • 1970-01-01
  • 2018-10-02
  • 2016-07-02
  • 2021-02-11
  • 1970-01-01
相关资源
最近更新 更多