17370845950

Spring Boot视频流:解决NullPointerException

本文旨在解决Spring Boot视频流应用中常见的`NullPointerException`问题,该问题通常出现在尝试从classpath加载视频资源时。我们将分析导致错误的根本原因,并提供明确的解决方案,确保视频流应用能够正确加载和播放视频文件。通过本文,开发者可以避免类似错误,并构建稳定可靠的视频流服务。

在构建Spring Boot视频流应用时,从classpath加载视频资源是常见的需求。然而,如果ResourceLoader未正确初始化,可能会导致NullPointerException。以下将详细分析问题原因及解决方案。

问题分析

错误信息java.lang.NullPointerException: null at net.javaguides.springboot.implementations.StreamingService.lambda$0(StreamingService.java:17)表明,在StreamingService类的getVideo方法中,resourceLoader实例为null,导致调用其getResource方法时抛出空指针异常。

这段代码是导致问题的核心:

package net.javaguides.springboot.implementations;

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import reactor.core.publisher.Mono;

@Service
public class StreamingService {

  private static final String FORMAT = "classpath:videos/%s.mp4";
  private ResourceLoader resourceLoader;

  public Mono getVideo(String title) {

    return Mono.fromSupplier(() -> resourceLoader.getResource(String.format(FORMAT, title)));

  }
}

问题在于private ResourceLoader resourceLoader;这行代码仅仅声明了一个ResourceLoader类型的变量,但没有对其进行初始化。因此,当Spring容器创建StreamingService的实例时,resourceLoader的值默认为null。

解决方案

解决此问题的关键在于使用Spring的依赖注入机制,确保ResourceLoader在StreamingService被创建时自动注入一个有效的实例。

修改代码如下:

package net.javaguides.springboot.implementations;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import reactor.core.publisher.Mono;

@Service
public class StreamingService {

  private static final String FORMAT = "classpath:videos/%s.mp4";
  @Autowired
  private ResourceLoader resourceLoader;

  public Mono getVideo(String title) {

    return Mono.fromSupplier(() -> resourceLoader.getResource(String.format(FORMAT, title)));

  }
}

通过在private ResourceLoader resourceLoader;声明前添加@Autowired注解,我们告诉Spring容器,在创建StreamingService的实例时,自动将一个ResourceLoader的实例注入到resourceLoader变量中。Spring会自动查找并注入一个合适的ResourceLoader实现。

代码解释

  • @Autowired: 这是Spring的依赖注入注解。它指示Spring容器自动装配一个bean到被注解的字段、构造器或setter方法中。
  • ResourceLoader: 这是Spring提供的接口,用于加载资源,例如classpath中的文件、URL资源等。

完整示例

以下是包含Controller和配置类的完整示例,以供参考:

// EndpointConfig.java
package net.javaguides.springboot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import net.javaguides.springboot.implementations.StreamingService;
import reactor.core.publisher.Mono;

@Configuration
public class EndPointConfig {
  @Autowired
  private StreamingService service;

  @Bean
  public RouterFunction router() {
    return RouterFunctions.route().GET("video/{title}", this::videoHandler).build();
  }

  private Mono videoHandler(ServerRequest serverRequest) {
    String title = serverRequest.pathVariable("title");
    return ServerResponse.ok().contentType(MediaType.valueOf("video/mp4")).body(service.getVideo(title),
        Resource.class);
  }

}

// VideoController.java
package net.javaguides.springboot.controller;

import lombok.AllArgsConstructor;
import net.javaguides.springboot.implementations.StreamingService;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@Controller
@AllArgsConstructor
public class VideoController {
  private StreamingService streamingService;

  @GetMapping(value = "video/{title}", produces = "video/mp4")
  @ResponseBody
  public Mono getVideos(@PathVariable String title, @RequestHeader("Range") String range) {
    System.out.println("Range in bytes = " + range);

    return streamingService.getVideo(title);

  }
  @GetMapping("show")
  public String show() {
    return "video";
  }
}

注意事项

  • 确保Spring Boot的版本兼容。
  • 检查classpath路径是否正确,视频文件是否位于src/main/resources/videos/目录下。
  • 如果仍然出现问题,检查Spring Boot的配置是否正确,例如是否启用了自动装配。

总结

通过使用@Autowired注解,我们可以确保ResourceLoader在StreamingService中被正确初始化,从而避免NullPointerException。这是一种常见的Spring Boot开发技巧,可以应用于其他需要依赖注入的场景。在开发过程中,仔细检查依赖注入是否正确配置,可以有效避免类似错误的发生。