Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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 已加载基于xml的spring mvc时,扫描手动添加的新控制器_Java_Spring_Rest_Api_Spring Mvc - Fatal编程技术网

Java 已加载基于xml的spring mvc时,扫描手动添加的新控制器

Java 已加载基于xml的spring mvc时,扫描手动添加的新控制器,java,spring,rest,api,spring-mvc,Java,Spring,Rest,Api,Spring Mvc,我正在从事一个项目,我们使用SpringMVC作为web框架。 它具有基于xml的配置,并首先启动。 但我也有插件,我可以手动添加到我的项目,而与它一起工作。每个插件都描述了一个API及其所有@Controller-s和模型 我设法在Spring配置中注册了这些API-s ( ),, 但我怎么能“唤醒”我的弹簧,说“请扫描所有这些控制器” 我的所有API-s都有一个ExceptionHandler,这就是为什么我需要扫描它们来连接这些控制器和处理程序 我试过这个,但不起作用 Annotation

我正在从事一个项目,我们使用SpringMVC作为web框架。 它具有基于xml的配置,并首先启动。 但我也有插件,我可以手动添加到我的项目,而与它一起工作。每个插件都描述了一个API及其所有@Controller-s和模型

我设法在Spring配置中注册了这些API-s (

),, 但我怎么能“唤醒”我的弹簧,说“请扫描所有这些控制器”

我的所有API-s都有一个ExceptionHandler,这就是为什么我需要扫描它们来连接这些控制器和处理程序

我试过这个,但不起作用

AnnotationConfigWebApplicationContext ctx=new AnnotationConfigWebApplicationContext();
        ctx.scan(packageName);
        ctx.refresh();

我在执行过程中没有收到任何错误。

对所有API定义的控制器类使用@RestController注释

@RestController
public class exampleController {

}
对其他组件类使用@Component注释

@Component
public class Validations {

}
此外,您还可以对定义了业务逻辑的业务层使用@Service注释,并对访问数据库的类使用@Repository注释

要启用上面提到的注释,如果您使用“maven”构建项目,则需要添加下面提到的依赖性。否则,将所有jar文件添加到与以下依赖性相关的项目中

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

org.springframework.boot
SpringBootStarterWeb
如果您需要处理异常,您需要创建下面提到的带有@ControllerAdvice注释的类。您可以为每个异常处理特定的方法。(示例:NullPointerException)

@ControllerAdvice
公共类ExceptionHandler扩展RuntimeException{
@自动连线
ResponceBuilder ResponceBuilder;
@杰索尼奥雷
私有HttpStatus status=HttpStatus.OK;
公共例外处理程序(){
超级();
}
@org.springframework.web.bind.annotation.ExceptionHandler(Example.class)
公共责任
NullPointerException(NullPointerException e){
info(“无效数据:{}”,例如getErrorMessage());
返回新的ResponseEntity(responseBuilder.build(e.getErrorCode(),
e、 getErrorMessage()),状态);
}

您是否尝试将其包含在您的xml配置文件中?是的,我为所有插件的基本包添加了组件扫描,但它不起作用。我不使用spring boot。我已经尝试放置Controller和ControllerAdvice批注。但是它对我没有帮助:\
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
@ControllerAdvice
public class ExceptionHandler extends RuntimeException {

@Autowired
ResponceBuilder responseBuilder;

@JsonIgnore
private HttpStatus status = HttpStatus.OK;

public ExceptionHandler() {
    super();
}

@org.springframework.web.bind.annotation.ExceptionHandler(Example.class)
public ResponseEntity<Response> 
NullPointerException(NullPointerException e) {

    log.info("Invalid Data: {}",e.getErrorMessage());
    return new ResponseEntity<>(responseBuilder.build(e.getErrorCode(), 
    e.getErrorMessage()), status);
}