简体   繁体   中英

How to import javax.validation(for using @Valid) in gradle project

I am building a simple login and signup page with web security. This is AuthController.java gives error as The import javax.validation.Valid cannot be resolved. I have added dependency in build.graddle and build the project then also giving the same error.

    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 code is attached below. Here I need to add anything more to import 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()
  }

Just use:

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

Link

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