gpt4 book ai didi

java - 简单的 Spring 应用程序返回路由错误

转载 作者:行者123 更新时间:2023-12-01 21:13:14 25 4
gpt4 key购买 nike

我创建了一个简单的 Spring 应用程序,其中一条路由位于“/a”,但是当我尝试访问该路由时,出现错误。日志显示正在映射路线,因此我不确定导致错误的原因。日志中没有错误信息输出。

HomeController.java

package com.12345;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

static Logger log = Logger.getLogger(HomeController.class.getName());

@RequestMapping("/a")
public String home() {
log.debug("a route reached");
return "Home";
}
}

应用程序.java

package com.12345.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.12345")
@EnableConfigurationProperties
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

堆栈跟踪和输出

2016-11-22 10:36:40.512  INFO 644 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@59fd97a8: startup date [Tue Nov 22 10:36:35 PST 2016]; root of context hierarchy
2016-11-22 10:36:40.615 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/a]}" onto public java.lang.String com.12345.HomeController.home()
2016-11-22 10:36:40.620 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-11-22 10:36:40.620 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-11-22 10:36:40.665 INFO 644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-22 10:36:40.665 INFO 644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-22 10:36:40.717 INFO 644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-22 10:36:41.045 INFO 644 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-11-22 10:36:41.137 INFO 644 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8085 (http)
2016-11-22 10:36:41.144 INFO 644 --- [ main] com.12345.config.Application : Started Application in 5.751 seconds (JVM running for 6.18)
2016-11-22 10:36:48.217 INFO 644 --- [nio-8085-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-11-22 10:36:48.217 INFO 644 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-11-22 10:36:48.242 INFO 644 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 25 ms

===============
localhost:8085/a
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Nov 22 10:47:39 PST 2016
There was an unexpected error (type=Not Found, status=404).

No message available

最佳答案

我一直使用注释:

@RestController
@RequestMapping("")

在 Controller 类上用于访问 Controller 并始终工作。

你试试怎么样:

package com.12345;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("")
public class HomeController {

static Logger log = Logger.getLogger(HomeController.class.getName());

@RequestMapping("/a")
public String home() {
log.debug("a route reached");
return "Home";
}
}

然后在我的应用程序中:

@SpringBootApplication  

这应该可以解决问题:

package com.12345.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@ComponentScan(basePackages="com.12345")
@EnableConfigurationProperties
@EnableAutoConfiguration
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

关于java - 简单的 Spring 应用程序返回路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com