【问题标题】:How to import javax.validation(for using @Valid) in gradle project如何在 gradle 项目中导入 javax.validation(用于使用 @Valid)
【发布时间】:2020-12-09 05:14:44
【问题描述】:

我正在构建一个带有网络安全性的简单登录和注册页面。 这是 AuthController.java 给出的错误为 导入 javax.validation.Valid 无法解析。我在 build.graddle 中添加了依赖项并构建了项目,然后也给出了同样的错误。

    package com.djamware.springsecuritymongodb.controllers;

    import com.djamware.springsecuritymongodb.domain.User;

    import com.djamware.springsecuritymongodb.services.CustomUserDetailsService;
    import javax.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Controller;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;

    @Controller
    public class AuthController {
    @Autowired
    private CustomUserDetailsService userService;
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView login() {
     ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("login");
    return modelAndView;
}
@RequestMapping(value = "/signup", method = RequestMethod.GET)
public ModelAndView signup() {
    ModelAndView modelAndView = new ModelAndView();
    User user = new User();
    modelAndView.addObject("user", user);
    modelAndView.setViewName("signup");
    return modelAndView;
}
@RequestMapping(value = "/signup", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
    ModelAndView modelAndView = new ModelAndView();
    User userExists = userService.findUserByEmail(user.getEmail());
    if (userExists != null) {
        bindingResult
                .rejectValue("email", "error.user",
                        "There is already a user registered with the username provided");
    }
    if (bindingResult.hasErrors()) {
        modelAndView.setViewName("signup");
    } else {
        userService.saveUser(user);
        modelAndView.addObject("successMessage", "User has been registered successfully");
        modelAndView.addObject("user", new User());
        modelAndView.setViewName("login");

    }
    return modelAndView;
}
@RequestMapping(value = "/dashboard", method = RequestMethod.GET)
public ModelAndView dashboard() {
    ModelAndView modelAndView = new ModelAndView();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = userService.findUserByEmail(auth.getName());
    modelAndView.addObject("currentUser", user);
    modelAndView.addObject("fullName", "Welcome " + user.getFullname());
    modelAndView.addObject("adminMessage", "Content Available Only for Users with Admin Role");
    modelAndView.setViewName("dashboard");
    return modelAndView;
}
@RequestMapping(value = {"/","/home"}, method = RequestMethod.GET)
public ModelAndView home() {
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("home");
    return modelAndView;
 }

}

Build.gradle 代码附在下面。在这里我还需要添加什么来导入 javax.validation?

    plugins {
id 'org.springframework.boot' version '2.4.0'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'war'
    }

    group = 'com.djamware'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '8'

    repositories {
    mavenCentral()
    }

   dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
   implementation 'org.springframework.boot:spring-boot-starter-security'
   implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
   implementation 'org.springframework.boot:spring-boot-starter-web'
   compile 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.4.1'
   // https://mvnrepository.com/artifact/javax.validation/validation-api
   compile group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final'

   // https://mvnrepository.com/artifact/javax.transaction/javax.transaction-api
  compile group: 'javax.transaction', name: 'javax.transaction-api', version: '1.3'
  compile group: 'javax.persistence', name: 'persistence-api', version: '1.0'

   implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
   providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
   testImplementation 'org.springframework.security:spring-security-test'
   }

  test {
  useJUnitPlatform()
  }

【问题讨论】:

  • 这是用于实验还是实际使用?请注意,您可以简单地将Authentication auth 添加为方法参数,Spring 会将其提供给您。

标签: java spring spring-boot gradle spring-security


【解决方案1】:
implementation 'org.springframework.boot:spring-boot-starter-validation:2.4.0'

Link

【讨论】:

    【解决方案2】:

    只需使用:

    compile 'org.springframework.boot:spring-boot-starter-validation:2.3.0.RELEASE'
    

    【讨论】:

    • javax.validation.Valid 不可用,除了这些都可用,如 javax.validation.bootstrap、约束、组、spi。
    猜你喜欢
    • 2021-01-19
    • 2022-01-11
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多