To use Spring Boot the best way is to use/import spring boot starter dependencies, see pom.xml definition example.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
In your example I made some changes, see below:
Normally the name of an domain object is singular, use User instead Users
@Entity
public class User {
@Id
...
private Integer id;
private String token;
...
//Getters and Setters
}
Change repository to remove @Transactional and fix @param (lower) and query errors.
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
public User findByEmail(String email);
public User findByToken(String token);
@Query("update User u set u.active = true where u.token = :token and u.id = :id")
@Modifying
public void activeUserByTokenAndId(@Param("token")String token, @Param("id")int id);
}
Add service implementation
@Service
public class UserService {
private final UserRepository userRepository;
public UserService(final UserRepository userRepository){
this.userRepository = userRepository;
}
@Transactional
public void activeUser(final String token){
User user= userRepository.findByToken(token);
if (users ==null){
// TODO throw exception like
// throw new UserNotFoundException();
}
this.userRepository.activeUserByTokenAndId(token, id);
}
}
Change controller to use service
@RestController
public class ActivationController {
private final UserService userService;
public ActivationController (final UserService userService){
this.userService = userService;
}
@GetMapping(value = "/activation/{token}")
public String activeUsersByToken(@PathVariable("token")String token){
userService.activeUser(token);
return "redirect:/login";
}
}
Run application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}