英文:
ResourceHttpRequestHandler: Resource not found, DispatcherServlet: No view rendering, null ModelAndView returned
问题
你的Spring应用似乎出现了问题。以下是可能出现问题的一些方面:
-
路径映射问题:当你访问
localhost:8080/creditcardslist
时,应用程序无法找到相应的资源。这可能是由于控制器路径映射配置不正确引起的。请确保CreditCardController
中的@GetMapping("/creditcardslist/")
路径映射与你的期望路径匹配。 -
资源文件问题:在访问
localhost:8080/creditcardslist
时,应用程序似乎正在寻找资源文件,但找不到它们。请确保你没有意外地将CreditCard相关的资源文件放置在错误的位置。 -
依赖注入问题:在
CreditCardController
中,你使用了CreditCardService
,但没有看到这个服务的实例化。你需要确保已经正确注入CreditCardService
,否则将无法调用getAll()
方法。 -
数据库配置问题:如果你使用了数据库,你需要确保数据库配置正确。在应用启动时,它应该能够连接到数据库并执行相应的操作。
请仔细检查这些方面,看看是否可以找到问题的根本原因。如果问题仍然存在,你可能需要提供更多关于你的应用程序配置和错误消息的详细信息,以便更好地诊断问题。
英文:
I am trying to do a simple Spring app which gets all the entries inside a table calls "creditcards".
This is the folder organization:
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
}
}
If I go to localhost:8080/ it works and it displays "Hello World", but if I go to localhost:8080/creditcardslist, it says:
and in the trace-logging-level console it displays:
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
and these are the logs when I go to localhost:8080/ and it correctly displays "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
What am I doing wrong?
答案1
得分: 1
你的端点目前以斜杠结尾,如下所示:@GetMapping("/creditcardslist/")
,你可以将它修改为/creditcardslist
。祝一切顺利。
英文:
It looks like your endpoint ending with a slash @GetMapping("/creditcardslist/")
Can you change it to /creditcardslist
?
Kind regards.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论