简体   繁体   中英

String boot getVideo always returns NullPointerException

Good Morning. Can someone help me please? I followed this tutorial https://www.knowledgefactory.net/2021/09/spring-boot-webflux-video-streaming.html to create a video streaming app. Inside the resources folder there is a subfolder which has two MP4 files inside. The problem happens when I hit the getVideo inside the next class:

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<Resource> getVideo(String title) {

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

  }
}

The controller uses a configuration bean which is as follows:

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<ServerResponse> router() {
    return RouterFunctions.route().GET("video/{title}", this::videoHandler).build();
  }

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

}

And Controller endpoint that retrieves video stream is as follows:

@Controller

@AllArgsConstructor
public class VideoController {
private StreamingService streamingService;

@GetMapping(value = "video/{title}", produces = "video/mp4")
  @ResponseBody
  public Mono<Resource> 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";
  }
}

And finally, the application has an html file that has a video tag whose src attribute points to the show method of the controller to display the player as follows:

<!DOCTYPE html>
<html
  lang="en"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorate="~{layout_navigation.html}">
<head>
<meta charset="UTF-8">
<title>OBR-VIDEOTHEQUE</title>
<meta
  name="viewport"
  content="width=device-width, initial-scale=1.0">
<!--    <link rel="stylesheet" href="styles.css"> -->
<style type="text/css">
#video-player {
    display: none;
}

#video-form {
    width: 60%;
}

.error {
    color: red;
}

.success {
    color: green;
}
</style>
</head>
<body>
  <section layout:fragment="content">
    <meta charset="utf-8">
    
      <div class="container mt-5">
      <h2>Video streaming</h2>
        <video src="video/movie" type="video/mp4" width="720" height="480" controls preload="none"> 
          
        </video>
      </div>
    
  </section>
  <section layout:fragment="footer">
    <footer class="container py-5 text-center footer">
      <div class="row">
        <div class="col-md-12">
          <p class="text-muted">&copy; 2022. Tous
            droits réservés.</p>
        </div>
      </div>
    </footer>
  </section>
  <section layout:fragment="scripts">
    <script th:src="@{/js/jQuery-min.js}"></script>
    <script
      type="text/javascript"
      th:src="@{/js/dropdown.js}"></script>
    <script th:src="@{/js/bootstrap.bundle.js}"></script>
    <script th:src="@{/js/bootstrap.min.js}"></script>
    <script th:src="@{/js/select2.min.js}"></script>
    <!--     <script th:src="@{/js/main.js}"></script> -->
  </section>
</body>
</html>

After running the application, in the url i type http://localhost:8082/show From that Url, the video player shows up. When I hit the play button, i get 500 Error saying this:

java.lang.NullPointerException: null
    at net.javaguides.springboot.implementations.StreamingService.lambda$0(StreamingService.java:17)

This is third day i tried to spot where is the error with no success. Can someone tell me where I'm getting things wrong.

I highly thank in advance anybody that will take time to help me. Regards,

You have a not initialized ResourceLoader .

Change this line of code

private ResourceLoader resourceLoader;

in these ones:

@Autowired
private ResourceLoader resourceLoader;

This will tell Spring Context to inject an instance of ResourceLoader in your attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM