Fix "No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse" in Spring Boot application

huangapple go评论70阅读模式
英文:

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 = &quot;is_user_admin&quot;
)
boolean isAdmin;
@Column(
name = &quot;username&quot;
)
@NotEmpty
@Pattern(regexp = &quot;^[a-zA-Z0-9_]+$&quot;, message = &quot;Only letters, digits and underscores allowed&quot;)
private String username;
@Column(
name =&quot;password&quot;
)
@Size(min = 5, message = &quot;your password must have at least 5 characters&quot;)
private String password;
@Transient
private String password2;
@OneToMany(mappedBy = &quot;user&quot;)
private List&lt;Entry&gt; entry;
@OneToMany(mappedBy = &quot;user&quot;)
private List&lt;Comment&gt; 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(&quot;/login&quot;)
public String login(Model model) {
model.addAttribute(&quot;login&quot;, new User (&quot;&quot;, &quot;&quot;));
return &quot;login&quot;;
}
@PostMapping(&quot;/login&quot;)
public String login(@ModelAttribute(&quot;login&quot;) User user, BindingResult bindingResult, HttpServletResponse response) {
Optional&lt;User&gt; 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(&quot;sessionId&quot;, session.getId());
response.addCookie(cookie);
// Login successful
return &quot;redirect:/&quot;;
}
bindingResult.addError(new FieldError (&quot;login&quot;, &quot;password&quot;, &quot;Login not successful.&quot;));
return &quot;login&quot;;
}
@PostMapping(&quot;/logout&quot;)
public String logout(@CookieValue(value = &quot;sessionId&quot;, defaultValue = &quot;&quot;) String sessionId, HttpServletResponse response) {
Optional&lt;Session&gt; optionalSession = webBlogServices.findByIdAndExpiresAtAfter(sessionId, Instant.now());
optionalSession.ifPresent(session -&gt; webBlogServices.delete(session));
Cookie cookie = new Cookie(&quot;sessionId&quot;, &quot;&quot;);
cookie.setMaxAge(0);
response.addCookie(cookie);
return &quot;redirect:/&quot;;
}

}


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:
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;3.0.2&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.thalia&lt;/groupId&gt;
&lt;artifactId&gt;library&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;library&lt;/name&gt;
&lt;description&gt;library&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;17&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.mysql&lt;/groupId&gt;
&lt;artifactId&gt;mysql-connector-j&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mariadb.jdbc&lt;/groupId&gt;
&lt;artifactId&gt;mariadb-java-client&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.postgresql&lt;/groupId&gt;
&lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;jakarta.servlet&lt;/groupId&gt;
&lt;artifactId&gt;jakarta.servlet-api&lt;/artifactId&gt;
&lt;version&gt;4.0.4&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;junit&lt;/groupId&gt;
&lt;artifactId&gt;junit&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;configuration&gt;
&lt;excludes&gt;
&lt;exclude&gt;
&lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
&lt;artifactId&gt;lombok&lt;/artifactId&gt;
&lt;/exclude&gt;
&lt;/excludes&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;
</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;

huangapple
  • 本文由 发表于 2023年2月27日 01:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定