我正在尝试做一个简单的Spring应用程序,它可以获取一个名为“creditcards”的表中的所有条目。
这是文件夹组织:
CreditCardsManagerApplication.java:
package com.progetto.creditcardsmanagerApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CreditCardsManagerApplication {
public static void main(String[] args) {
SpringApplication.run(CreditCardsManagerApplication.class, args);
}
}
CreditCard.java:
package com.progetto.creditcardsmanagerApp.creditcards;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name="creditcards")
public class CreditCard {
@Id
private Integer id;
private String creditcardnumber;
private String owner;
public CreditCardDTO toDTO(){
return new CreditCardDTO(this.creditcardnumber,this.owner);
}
}
CreditCardDTO.java:
package com.progetto.creditcardsmanagerApp.creditcards;
public class CreditCardDTO{
private String creditCardNumber;
private String owner;
public CreditCardDTO(String creditCardNumber, String owner){
this.creditCardNumber = creditCardNumber;
this.owner = owner;
}
}
CreditCardRepository.java:
package com.progetto.creditcardsmanagerApp.creditcards;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CreditCardRepository extends JpaRepository<CreditCard, Integer> {
}
CreditCardService.java:
package com.progetto.creditcardsmanagerApp.creditcards;
import java.util.List;
public interface CreditCardService {
public List<CreditCardDTO> getAll();
}
CreditCardServiceImpl.java:
package com.progetto.creditcardsmanagerApp.creditcards;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;
@Service
public class CreditCardServiceImpl implements CreditCardService{
private CreditCardRepository creditCardRepository;
@Override
public List<CreditCardDTO> getAll() {
List<CreditCardDTO> l = new LinkedList<CreditCardDTO>();
for (CreditCard cr: this.creditCardRepository.findAll()) {
l.add(cr.toDTO());
}
System.out.println(l.toString());
return l;
}
}
CreditCardcontroller.java:
package com.progetto.creditcardsmanagerApp.creditcards;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class CreditCardController {
private CreditCardService creditCardService;
@GetMapping("/creditcardslist/")
public List<CreditCardDTO> getAll(){
return this.creditCardService.getAll();
}
@GetMapping("/")
public String test(){
return "Hello World"; // THIS WORKS
}
}
如果我转到localhost:8080/,它会工作并显示“Hello World”,但如果我转到localhost:8080/creditcardslist,它会说:
在跟踪日志级别控制台中,它显示:
2023-06-28T22:55:10.153+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.a.c.authenticator.AuthenticatorBase : Security checking request GET /creditcardslist
2023-06-28T22:55:10.153+02:00 DEBUG 1683317 --- [nio-8080-exec-2] org.apache.catalina.realm.RealmBase : No applicable constraints defined
2023-06-28T22:55:10.153+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.a.c.authenticator.AuthenticatorBase : Not subject to any constraint
2023-06-28T22:55:10.153+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.apache.catalina.core.StandardWrapper : Returning instance
2023-06-28T22:55:10.153+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.s.b.w.s.f.OrderedRequestContextFilter : Bound request context to thread: org.apache.catalina.connector.RequestFacade@75462fa7
2023-06-28T22:55:10.153+02:00 DEBUG 1683317 --- [nio-8080-exec-2] org.apache.tomcat.util.http.Parameters : Set encoding to UTF-8
2023-06-28T22:55:10.154+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/creditcardslist", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2023-06-28T22:55:10.154+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]] and 4 interceptors
2023-06-28T22:55:10.154+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-06-28T22:55:10.155+02:00 TRACE 1683317 --- [nio-8080-exec-2] .i.SessionFactoryImpl$SessionBuilderImpl : Opening Hibernate Session. tenant=null
2023-06-28T22:55:10.155+02:00 TRACE 1683317 --- [nio-8080-exec-2] org.hibernate.internal.SessionImpl : Opened Session [e30f86c1-e4f4-4874-ac25-242dda4c2ab2] at timestamp: 1687985710155
2023-06-28T22:55:10.155+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2023-06-28T22:55:10.155+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : No view rendering, null ModelAndView returned.
2023-06-28T22:55:10.155+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-06-28T22:55:10.155+02:00 TRACE 1683317 --- [nio-8080-exec-2] org.hibernate.internal.SessionImpl : Closing session [e30f86c1-e4f4-4874-ac25-242dda4c2ab2]
2023-06-28T22:55:10.155+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@7b81df7f]
2023-06-28T22:55:10.155+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2023-06-28T22:55:10.156+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.h.r.j.i.LogicalConnectionManagedImpl : Closing logical connection
2023-06-28T22:55:10.156+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.h.r.j.i.LogicalConnectionManagedImpl : Logical connection closed
2023-06-28T22:55:10.156+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND, headers={masked}
2023-06-28T22:55:10.156+02:00 TRACE 1683317 --- [nio-8080-exec-2] o.s.b.w.s.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@75462fa7
2023-06-28T22:55:10.156+02:00 DEBUG 1683317 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost] : Processing ErrorPage[errorCode=0, location=/error]
etc etc
这些是当我转到localhost:8080/并正确显示“Hello World”时的日志:
2023-06-28T22:23:30.281+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2023-06-28T22:23:30.293+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'creditCardController'
2023-06-28T22:23:30.294+02:00 TRACE 1663300 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.progetto.creditcardsmanagerApp.creditcards.CreditCardController#test()
2023-06-28T22:23:30.296+02:00 DEBUG 1663300 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-06-28T22:23:30.296+02:00 TRACE 1663300 --- [nio-8080-exec-1] .i.SessionFactoryImpl$SessionBuilderImpl : Opening Hibernate Session. tenant=null
2023-06-28T22:23:30.296+02:00 TRACE 1663300 --- [nio-8080-exec-1] org.hibernate.internal.SessionImpl : Opened Session [66de72e4-de4f-4e42-8402-61252358db48] at timestamp: 1687983810296
2023-06-28T22:23:30.309+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.s.web.method.HandlerMethod : Arguments: []
2023-06-28T22:23:30.336+02:00 DEBUG 1663300 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Using 'text/html', given [text/html, application/xhtml+xml, image/avif, image/webp, application/xml;q=0.9, */*;q=0.8] and supported [text/plain, */*, application/json, application/*+json]
2023-06-28T22:23:30.337+02:00 TRACE 1663300 --- [nio-8080-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Writing ["Hello World"]
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerAdapter : Applying default cacheSeconds=-1
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : No view rendering, null ModelAndView returned.
2023-06-28T22:23:30.345+02:00 DEBUG 1663300 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] org.hibernate.internal.SessionImpl : Closing session [66de72e4-de4f-4e42-8402-61252358db48]
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.h.e.jdbc.internal.JdbcCoordinatorImpl : Closing JDBC container [org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl@5f7b4697]
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.h.r.j.i.ResourceRegistryStandardImpl : Releasing JDBC resources
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.h.r.j.i.LogicalConnectionManagedImpl : Closing logical connection
2023-06-28T22:23:30.345+02:00 TRACE 1663300 --- [nio-8080-exec-1] o.h.r.j.i.LogicalConnectionManagedImpl : Logical connection closed
2023-06-28T22:23:30.346+02:00 DEBUG 1663300 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 200 OK, headers={masked}
etc etc
我做错了什么?
1条答案
按热度按时间im9ewurl1#
它看起来像你的端点以斜杠@GetMapping(“/creditcardslist/”)结尾,你可以将它更改为/creditcardslist吗?
亲切的问候