首页 > 解决方案 > 分级测试中的错误。无限递归错误

问题描述

我正在在线学习 Spring MVC。作为课程的一部分,我必须开发云视频服务。这里提到了规格。 https://github.com/juleswhite/mobile-cloud-asgn1

下面是我的控制器类。

@Controller

公共类 VideosController { private final AtomicLong currentId = new AtomicLong(1L);

//A Map to hold incoming Video meta data
private HashMap<Long, Video> videoMap = new HashMap<Long, Video>(); 

//Receives GET requests to /video and returns the current list
// list of videos in memory
@RequestMapping(value = "/video", method = RequestMethod.GET)
public @ResponseBody List<Video> getVideoList() throws IOException{
    List<Video> resultList = new ArrayList<Video>();        
    for(Long id : videoMap.keySet()) {
        resultList.add(videoMap.get(id));
    }       
    return resultList;
}

//Receives POST requests to /video and adds the video object
//created from request data to the Map
@RequestMapping(value = "/video", method = RequestMethod.POST)
public @ResponseBody() Video addVideoMetadata(@RequestBody Video data)
{   
    //create a Video object     
    Video video = Video.create().withContentType(data.getContentType())
            .withDuration(data.getDuration())
            .withSubject(data.getSubject())
            .withTitle(data.getTitle()).build();

    //set the id for the video
    long videoId = currentId.incrementAndGet();
    video.setId(videoId);

    //set the URL for this Video
    String videoURL = getDataUrl(videoId);
    video.setDataUrl(videoURL);         
    //save the Video metadata object to map
    Video v = save(video);
    return v;       
}

// Receives POST requests to /video/{id}/data e.g. /videoa/2/data
// uploads the video file sent as MultipartFile 
// and writes it to the disc
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.POST)    
public  @ResponseBody ResponseEntity<VideoStatus> uploadVideo
        (@RequestParam("data") MultipartFile data,
        @PathVariable("id") long id,
        HttpServletResponse response
        ) throws IOException 
{   
    // if video with id not present
    if(!videoMap.containsKey(id)) {
        System.out.println(" this id not present");         
        return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        //return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
    }
    InputStream in = null;
    try {
        //read the input stream
        in = data.getInputStream();
    }
    catch(IOException ie){
        System.out.println("Exception reading inputstream");
    }
    finally {
        in.close();
    }       
    //get the video
    Video v = videoMap.get(id); 
    //write it to disk
    VideoFileManager.get().saveVideoData(v, in);
    VideoStatus vs = new VideoStatus(VideoStatus.VideoState.READY); 
    return new ResponseEntity<>(vs, HttpStatus.OK);
    //response.setStatus(200);
    //return new ResponseEntity<>(vs, HttpStatus.OK);

}   

//Reads GET request to /vide/{id}/data and returns the video
//binary data as output stream
@RequestMapping(value = "/video/{id}/data", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<OutputStream> getBinaryData(
        @PathVariable("id") long videoId,
        HttpServletResponse response) throws IOException {

    //if id is incorrect 
    if(!videoMap.containsKey(videoId)) {
        return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
    }   
    //get the video from Map
    Video outVideo = videoMap.get(videoId);
    VideoFileManager vm = VideoFileManager.get();
    //write the binary data to OutputStream
    OutputStream os  = response.getOutputStream();
    vm.copyVideoData(outVideo, os);

    return new ResponseEntity<>(os, HttpStatus.OK);
}

//save incoming video metadata to a Map
public Video save(Video v) {
    checkAndSetId(v);
    if(!videoMap.containsKey(v.getId())) {
        videoMap.put(v.getId(), v);
    }
    return v;
}


//helper method to generate url for video
private String getDataUrl(long videoId){
    String url = getUrlBaseForLocalServer() + "/video/" + videoId + "/data";
    return url;
}

private String getUrlBaseForLocalServer() {
       HttpServletRequest request = 
           ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
       String base = 
          "http://"+request.getServerName() 
          + ((request.getServerPort() != 80) ? ":"+request.getServerPort() : "");
       return base;
}

private void checkAndSetId(Video entity) {
    if(entity.getId() == 0){
        entity.setId(currentId.incrementAndGet());
    }
}

}

现在,我通过了 AutoGradingTest.java 单元代码中的所有测试,但没有通过 testAddVideoData()。它给出了一个套接字超时错误,然后是无限递归错误,指向 AutoGradingTest.java 中的第 159 行 从逻辑上讲,我的代码似乎没问题。许多其他学习者也面临最口渴的情况,但课程讲师没有帮助。有人可以在这里帮助我吗?非常感谢。

标签: javaspringspring-mvcretrofit

解决方案


推荐阅读