在我的控制器中导入org.springframework.web
时遇到问题。我不知道发生了什么
我已经在intellij
中导入了该项目,并使用maven
进行了构建。
我还在项目结构中添加了spring。因此,我让maven包含所有必需的库并生成pom.xml
。
实际上,我正在尝试在JEE中构建rest api。
这是我的pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.university.rouen</groupId>
<artifactId>JEE</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>JEE Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>JEE</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
这是我的控制器:
package src.main.controllers;
import Database.src.entities.UserEntity;
import Database.src.repository.classes.UserRepository;
import factory.DataBaseSessionFactory;
import main.utils.PasswordSaltAndHash;
import main.utils.XMLObjects.LoginXMLObject;
import main.utils.XMLObjects.RegisterXMLObject;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.web.bind.annotation.*; // Get error : cannot resolved symbol 'web'
import JEE.Database.src.repository.interfaces.IUserRepository;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
class UserController {
private final Pattern EMAIL_REGEX = Pattern.compile("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$", Pattern.CASE_INSENSITIVE);
private final Pattern PASSWORD_REGEX = Pattern.compile("^[A-Za-z0-9]{8,16}$", Pattern.CASE_INSENSITIVE);
private IUserRepository _userRepository;
public UserController() {
_userRepository = new UserRepository();
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public String register(@RequestBody String content) {
RegisterXMLObject register = RegisterXMLObject.XMLToRegister(content);
if (register.getFirstName() == null || register.getFirstName().isEmpty()
|| register.getLastName() == null || register.getLastName().isEmpty()
|| register.getEmail() == null || register.getEmail().isEmpty()
|| register.getPassword() == null || register.getPassword().isEmpty()
|| register.getPasswordConfirm() == null || register.getPasswordConfirm().isEmpty()) {
return "All fields are required !";
}
Matcher matcher2 = PASSWORD_REGEX.matcher(register.getPassword());
if (!matcher2.matches()) {
return "Password is not valid!";
}
Matcher matcher1 = EMAIL_REGEX.matcher(register.getEmail());
if (!matcher1.matches()) {
return "Email address is not valid!";
}
if (!register.getPassword().equals(register.getPasswordConfirm())) {
return "The two passwords are not equal!";
}
final Session session = DataBaseSessionFactory.getSession();
UserEntity user = _userRepository.GetByEmail(register.getEmail(), session);
if (user != null) {
session.close();
return "This email address is already used!";
}
byte[] salt = new String(PasswordSaltAndHash.getNextSalt()).getBytes();
byte[] hash = new String(PasswordSaltAndHash.hash(register.getPassword().toCharArray(), salt)).getBytes();
UserEntity entity = new UserEntity();
entity.setFirstname(register.getFirstName());
entity.setLastname(register.getLastName());
entity.setEmail(register.getEmail());
entity.setPasswordHash(new String(hash));
entity.setPasswordSalt(new String(salt));
Transaction tx = session.beginTransaction();
_userRepository.Add(entity, session);
tx.commit();
session.close();
return "User created!";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestBody String content) {
LoginXMLObject login = LoginXMLObject.XMLToLogin(content);
if (login.getLogin() == null || login.getLogin().isEmpty()
|| login.getPassword() == null || login.getPassword().isEmpty()) {
return "All fields are required!";
}
final Session session = DataBaseSessionFactory.getSession();
UserEntity user = _userRepository.GetByEmail(login.getLogin(), session);
if (user == null) {
session.close();
return "This user doesn't exists!";
}
byte[] hash = new String(PasswordSaltAndHash.hash(login.getPassword().toCharArray(),
user.getPasswordSalt().getBytes())).getBytes();
if (!new String(hash).equals(user.getPasswordHash())) {
session.close();
return "Password is not valid!";
}
Transaction tx = session.beginTransaction();
byte[] token = new String(PasswordSaltAndHash.getNextSalt()).getBytes();
user.setToken(new String(token));
tx.commit();
session.close();
return user.getToken();
}
@RequestMapping(value = "/logout", method = RequestMethod.POST)
public String logout(@RequestBody String content) {
final Session session = DataBaseSessionFactory.getSession();
UserEntity user = _userRepository.GetByToken(content, session);
if (user == null) {
session.close();
return "This user doesn't exists!";
}
Transaction tx = session.beginTransaction();
user.setToken("");
tx.commit();
session.close();
return "User disconnected";
}
}
欢迎任何帮助