英文:
Fix "No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse" in Spring Boot application
问题
I want to build a Register and login module for my application and ran into the error in the title.
My login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form th:action="@{/login}" th:object="${login}" method="post">
Username: <input type="text" th:field="*{username}"><br/>
<p th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></p>
Password: <input type="password" th:field="*{password}"><br/>
<p th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></p>
<button type="submit">Login</button>
</form>
</body>
</html>
My User Class:
package com.webprojekt.webblog.DAO;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@Entity(name = "user")
@Table
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@SequenceGenerator(
name = "user_sequence",
sequenceName = "user_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "user_sequence"
)
@Column(
nullable = false
)
private long id;
@Column(
name = "name",
nullable = true,
columnDefinition = "TEXT"
)
private String name;
@Column(
name = "is_user_admin"
)
boolean isAdmin;
@Column(
name = "username"
)
@NotEmpty
@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "Only letters, digits and underscores allowed")
private String username;
@Column(
name ="password"
)
@Size(min = 5, message = "your password must have at least 5 characters")
private String password;
@Transient
private String password2;
@OneToMany(mappedBy = "user")
private List<Entry> entry;
@OneToMany(mappedBy = "user")
private List<Comment> comment;
public User(String name) {
this.name = name;
this.isAdmin=false;
}
//DTO Login
public User(String username, String password1) {
this.username = username;
this.password = password1;
}
//DTO registration
public User( String username, String password1, String password2) {
this.username = username;
this.password = password1;
this.password2 = password2;
}
public User(String name, String username, String password, String password2) {
this.name = name;
this.username = username;
this.password = password;
this.password2 = password2;
}
}
My Session Class:
package com.webprojekt.webblog.DAO;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.Instant;
import java.util.UUID;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table
@Data
@Entity
public class Session {
@Id
private String id = UUID.randomUUID().toString();
@ManyToOne
private User user;
private Instant expiresAt;
public Session(User user, Instant expiresAt) {
this.user = user;
this.expiresAt = expiresAt;
}
public User getUser() {
return user;
}
public String getId() {
return id;
}
public void setExpiresAt(Instant expiresAt) {
this.expiresAt = expiresAt;
}
}
And SessionController:
package com.webprojekt.webblog.API;
import com.webprojekt.webblog.BussinesLayer.WebBlogServices;
import com.webprojekt.webblog.DAO.Session;
import com.webprojekt.webblog.DAO.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.Optional;
@Controller
public class SessionController {
private WebBlogServices webBlogServices;
@Autowired
public SessionController(WebBlogServices webBlogServices) {
this.webBlogServices = webBlogServices;
}
@GetMapping("/login")
public String login(Model model) {
model.addAttribute("login", new User ("", ""));
return "login";
}
@PostMapping("/login")
public String login(@ModelAttribute("login") User user, BindingResult bindingResult, HttpServletResponse response) {
Optional<User> optionalUser = webBlogServices.findByUsernameAndPassword(user.getUsername(), user.getPassword ());
if (optionalUser.isPresent()) {
Session session = new Session(optionalUser.get(), Instant.now().plusSeconds(7*24*60*60)); //expires one week from now
webBlogServices.addSession(session);
//store the session ID in a cookie to keep the username secret
Cookie cookie = new Cookie("sessionId", session.getId());
response.addCookie(cookie);
// Login successful
return "redirect:/";
}
bindingResult.addError(new FieldError ("login", "password", "Login not successful."));
return "login";
}
@PostMapping("/logout")
public String logout(@CookieValue(value = "sessionId", defaultValue = "") String sessionId, HttpServletResponse response) {
Optional<Session> optionalSession = webBlogServices.findByIdAndExpiresAtAfter(sessionId, Instant.now());
optionalSession.ifPresent(session -> webBlogServices.delete(session));
Cookie cookie = new Cookie("sessionId", "");
cookie.setMaxAge(0);
response.addCookie(cookie);
return "redirect:/";
}
}
Console Log:
2023-02-26T18:44:00.439+01:00 ERROR 10952 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse] with root cause
java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse
at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:266) ~[spring-beans-6.0.4.jar:6.0.4]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219) ~[spring-web-6.0.
<details>
<summary>英文:</summary>
I want to build a Register and login module for my application and ran into the error in the title.
My login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form th:action="@{/login}" th:object="${login}" method="post">
Username: <input type="text" th:field="{username}"><br/>
<p th:if="${#fields.hasErrors('username')}" th:errors="{username}"></p>
Password: <input type="password" th:field="{password}"><br/>
<p th:if="${#fields.hasErrors('password')}" th:errors="{password}"></p>
<button type="submit">Login</button>
</form>
</body>
</html>
My User Class:
package com.webprojekt.webblog.DAO;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@Entity(name = "user")
@Table
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
@Id
@SequenceGenerator(
name = "user_sequence",
sequenceName = "user_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "user_sequence"
)
@Column(
nullable = false
)
private long id;
@Column(
name = "name",
nullable = true,
columnDefinition = "TEXT"
)
private String name;
@Column(
name = "is_user_admin"
)
boolean isAdmin;
@Column(
name = "username"
)
@NotEmpty
@Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "Only letters, digits and underscores allowed")
private String username;
@Column(
name ="password"
)
@Size(min = 5, message = "your password must have at least 5 characters")
private String password;
@Transient
private String password2;
@OneToMany(mappedBy = "user")
private List<Entry> entry;
@OneToMany(mappedBy = "user")
private List<Comment> comment;
public User(String name) {
this.name = name;
this.isAdmin=false;
}
//DTO Login
public User(String username, String password1) {
this.username = username;
this.password = password1;
}
//DTO registration
public User( String username, String password1, String password2) {
this.username = username;
this.password = password1;
this.password2 = password2;
}
public User(String name, String username, String password, String password2) {
this.name = name;
this.username = username;
this.password = password;
this.password2 = password2;
}
}
My Session Class:
package com.webprojekt.webblog.DAO;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.Instant;
import java.util.UUID;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table
@Data
@Entity
public class Session {
@Id
private String id = UUID.randomUUID().toString();
@ManyToOne
private User user;
private Instant expiresAt;
public Session(User user, Instant expiresAt) {
this.user = user;
this.expiresAt = expiresAt;
}
public User getUser() {
return user;
}
public String getId() {
return id;
}
public void setExpiresAt(Instant expiresAt) {
this.expiresAt = expiresAt;
}
}
And SessionController:
package com.webprojekt.webblog.API;
import com.webprojekt.webblog.BussinesLayer.WebBlogServices;
import com.webprojekt.webblog.DAO.Session;
import com.webprojekt.webblog.DAO.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.time.Instant;
import java.util.Optional;
@Controller
public class SessionController {
private WebBlogServices webBlogServices;
@Autowired
public SessionController(WebBlogServices webBlogServices) {
this.webBlogServices = webBlogServices;
}
@GetMapping("/login")
public String login(Model model) {
model.addAttribute("login", new User ("", ""));
return "login";
}
@PostMapping("/login")
public String login(@ModelAttribute("login") User user, BindingResult bindingResult, HttpServletResponse response) {
Optional<User> optionalUser = webBlogServices.findByUsernameAndPassword(user.getUsername(), user.getPassword ());
if (optionalUser.isPresent()) {
Session session = new Session(optionalUser.get(), Instant.now().plusSeconds(7*24*60*60)); //expires one week from now
webBlogServices.addSession(session);
//store the session ID in a cookie to keep the username secret
Cookie cookie = new Cookie("sessionId", session.getId());
response.addCookie(cookie);
// Login successful
return "redirect:/";
}
bindingResult.addError(new FieldError ("login", "password", "Login not successful."));
return "login";
}
@PostMapping("/logout")
public String logout(@CookieValue(value = "sessionId", defaultValue = "") String sessionId, HttpServletResponse response) {
Optional<Session> optionalSession = webBlogServices.findByIdAndExpiresAtAfter(sessionId, Instant.now());
optionalSession.ifPresent(session -> webBlogServices.delete(session));
Cookie cookie = new Cookie("sessionId", "");
cookie.setMaxAge(0);
response.addCookie(cookie);
return "redirect:/";
}
}
Console Log:
2023-02-26T18:44:00.439+01:00 ERROR 10952 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse] with root cause
java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse
at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:266) ~[spring-beans-6.0.4.jar:6.0.4]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:147) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:181) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:148) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.4.jar:6.0.4]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.4.jar:6.0.4]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731) ~[tomcat-embed-core-10.1.5.jar:6.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.4.jar:6.0.4]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.4.jar:6.0.4]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
at java.base/java.lang.Thread.run(Thread.java:1589) ~[na:na]
So far ive probably tried all solutions found through google, but didnt get a solution. Its my first time using technologies like Lombok, and HttpServletResponse. I know that i cannot instanciate an interface in java, but i dont know how to solve it anyways. Thank you guys in advance.
edit1:
My 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.thalia</groupId>
<artifactId>library</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>library</name>
<description>library</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
</details>
# 答案1
**得分**: 9
Spring Boot 3(和Spring Framework 6)需要Jakarte EE 10的基线。您不能将其与低于此版本的Java EE或Jakarte EE版本一起使用。
您必须从您的`pom.xml`中移除对`jakarta.servlet-api`的显式依赖。Java Servlet 4位于基线以下,特别是仍然使用以`javax.servlet`开头的包名。
如果您删除了显式依赖,Spring会传递引入正确的依赖。然后,您需要将所有以`javax.servlet`开头的导入替换为`javax`替换为`jakarta`,例如:
```java
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
英文:
Spring Boot 3 (and Spring Framework 6) require a baseline of Jakarte EE 10. You cannot use it with Java EE or Jakarte EE versions below that.
You have to remove the explicit dependency on jakarta.servlet-api
from your pom.xml
. Java Servlet 4 is below the baseline and in particular still uses the package names starting with javax.servlet
.
If you remove the explicit dependency, Spring will pull in transitively the correct one. You then need to replace all imports starting with javax.servlet
with javax
replaced by jakarta
, e.g.
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
答案2
得分: 1
将所有以 "javax.servlet" 开头的导入替换为 "jakarta",例如:
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
英文:
Replace all imports starting with javax.servlet with javax replaced by jakarta,
e.g.
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论