Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBean没有在SpringRest中初始化_Java_Spring - Fatal编程技术网

Java SpringBean没有在SpringRest中初始化

Java SpringBean没有在SpringRest中初始化,java,spring,Java,Spring,我的SpringBean没有在SpringJava配置中初始化,我正在使用该配置创建一个示例SpringREST应用程序。不需要Web.xml,我已将其删除。调用REST端点/dest/types时也会得到404 谁能帮忙吗 Pom.xml: 4.0.0 com.travel 帕西耶雷斯塔普 战争 0.0.1-快照 PatcyRestapp Maven Webapp http://maven.apache.org 5.0.9.1发布 2.9.6 1.8 1.8 5.3.6.最终版本 3.1.0

我的SpringBean没有在SpringJava配置中初始化,我正在使用该配置创建一个示例SpringREST应用程序。不需要Web.xml,我已将其删除。调用REST端点/dest/types时也会得到404

谁能帮忙吗

Pom.xml:

4.0.0 com.travel 帕西耶雷斯塔普 战争 0.0.1-快照 PatcyRestapp Maven Webapp http://maven.apache.org 5.0.9.1发布 2.9.6 1.8 1.8 5.3.6.最终版本 3.1.0 1.18.12 3.9 错误的 org.springframework 弹簧芯 ${springframework.version} org.springframework SpringWebMVC ${springframework.version} javax.servlet javax.servlet-api ${javax.servlet.api.version} com.fasterxml.jackson.core 杰克逊数据绑定 ${jackson.library} org.hibernate 冬眠核心 ${hibernate.core.version} org.projectlombok 龙目 ${lombok.version} 假如 org.apache.commons commons-lang3 ${apache.commons.version} 帕西耶雷斯塔普 分派初始值设定项:

包com.patcyy.rest.config; 导入org.springframework.web.servlet.support.AbstractAnnotationConfigDispatchersServletInitializer; 公共类PatcyDispatcherservletInitializer扩展 AbstractAnnotationConfigDispatchersServletInitializer{ @凌驾 受保护类[]GetRootConfigClass{ 返回null; } @凌驾 受保护类[]getServletConfigClasses{ System.out.PrintLnInner GetServletConfigClass; 返回新类[]{patcyConfig.Class}; } @凌驾 受保护的字符串[]getServletMappings{ System.out.printlnInside映射; 返回新字符串[]{/patcyy}; } } 配置:

包com.patcyy.rest.config; 导入org.springframework.context.annotation.ComponentScan; 导入org.springframework.context.annotation.Configuration; 导入org.springframework.web.servlet.config.annotation.EnableWebMvc; @配置 @EnableWebMvc @ComponentScancom.patcyy.rest 公共类PatcyConfig{ } 控制器:

包com.patcyy.rest.controller; 导入java.util.List; 导入org.springframework.beans.factory.annotation.Autowired; 导入org.springframework.http.HttpStatus; 导入org.springframework.http.MediaType; 导入org.springframework.http.ResponseEntity; 导入org.springframework.web.bind.annotation.GetMapping; 导入org.springframework.web.bind.annotation.RequestMapping; 导入org.springframework.web.bind.annotation.RestController; 导入com.patcyy.rest.response.DestinationTypes; 导入com.patcyy.rest.service.IDestinationService; @RestController @RequestMappingpath=/dest,consumes=MediaType.APPLICATION\u JSON\u UTF8\u值,Products= MediaType.APPLICATION\u JSON\u UTF8\u值 公共类DestinationController扩展BaseController{ 私人最终创意服务创意创意创意服务; /** *@param-iDestinationService */ 平民的DestinationController@AutowiredIDestinationService IDestinationService{ 超级的 this.iDestinationService=iDestinationService; } @获取映射/类型 公共响应getDestinationTypes{ List destTypes=iDestinationService.getDestinationTypes; 返回新的ResponseEntitydestTypes,HttpStatus.OK; } }
请共享控制台日志以便进一步调查。根据我看到以上代码后的理解,您可以替换

/帕西,只有一个孩子/ 在getServletMappings方法中

您请求的资源url如下所示:

http://{hostname:port}/patcyyRestApp/dest/types


这是由于无效的url servlet映射导致的

我认为问题在于组件扫描。 @ComponentScan注释和@Configuration注释一起指定要扫描的包@不带参数的ComponentScan告诉Spring扫描当前包及其所有子包。
在您的情况下,需要添加basepackages=com.patcyy.rest

我必须做两个修改来解决这个问题

因为我只使用Java配置,所以我删除了Web.xml。因此,我不得不在server.xml中对Tomcat进行如下更改:

我必须将Dispatcher Initializer中的servlet映射更新为: 返回新字符串[]{/patcyy/*}

在做了这两个更改之后,它现在运行良好


请查看此

您需要使用/patcy/dest/types not/dest/types@Raju谢谢你的帮助。但是我也试过了,但是没有用。@emotionlessbananas谢谢分享这个链接。学到了很多东西
it’经历了这一切之后,谢谢你的帮助。就我而言,原因是不同的。请看一下上面的答案。谢谢你的帮助。就我而言,原因是不同的。请看一下上面的答案。