gpt4 book ai didi

java - 其余 URL 路径未映射 ( Spring Boot )

转载 作者:行者123 更新时间:2023-12-02 02:32:07 25 4
gpt4 key购买 nike

考虑一下,按照我的问题进行类(class),

启动应用程序java类:

package com.abb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = {"com.abb.repositories"})
public class ServerApplication {

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

CrudRepository 接口(interface)(它刚刚添加到这里,因为我觉得包可能会导致这个问题):

package com.abb.repositories;

import org.springframework.data.repository.CrudRepository;
import com.abb.entities.XYZ;

public interface XYZRepository extends CrudRepository<XYZ, Long> { }

用于 REST API 的 Controller Java 类:

package com.abb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.abb.repositories.XYZRepository;

@RestController
@RequestMapping("/abc")
public class ABCController {

@Autowired
private XYZRepository xyzRepository;

@RequestMapping(value = "/test", method = RequestMethod.POST)
public @ResponseBody String test(@RequestBody int iValue) {
return "done" + iValue;
}
}

服务器日志:

2017-10-25 22:22:25.579  INFO 4664 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-10-25 22:22:26.291 INFO 4664 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4521e6e2: startup date [Wed Oct 25 22:22:17 IST 2017]; root of context hierarchy
2017-10-25 22:22:26.470 INFO 4664 --- [ restartedMain] 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)
2017-10-25 22:22:26.485 INFO 4664 --- [ restartedMain] 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)
2017-10-25 22:22:26.548 INFO 4664 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-25 22:22:26.548 INFO 4664 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-25 22:22:26.657 INFO 4664 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-25 22:22:27.250 INFO 4664 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2017-10-25 22:22:27.344 INFO 4664 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-10-25 22:22:27.470 INFO 4664 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-10-25 22:22:27.486 INFO 4664 --- [ restartedMain] com.yq.WhyqueueServerApplication : Started WhyqueueServerApplication in 10.814 seconds (JVM running for 11.612)

最佳答案

问题是您限制 Spring Boot 扫描仪仅扫描存储库包。不会扫描其他包裹。这意味着它不会获取位于“com.abb.controllers”包中的 Controller 。

删除 scanBasePackages 属性、替换值或添加您要扫描的每个单独的包。

我推荐这个:

@SpringBootApplication

或者这个:

@SpringBootApplication(scanBasePackages = {"com.abb"})

关于java - 其余 URL 路径未映射 ( Spring Boot ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46938537/

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