弹簧控制器

时间:2017-06-02 09:03:04

标签: spring rest spring-boot controller request-mapping

我想对我的弹簧启动控制器中始终抛出的错误发表意见。我得到以下第一个接受reading请求的控制器:

@RequestMapping(value="/read/{file:.+}" , method =  RequestMethod.GET)
public ResponseEntity myFunction(@PathVariable("file") String file) {

    String[] parts = file.split("\\.");
    String extension = parts[1];
    List<SousBloc> resWord;
    List<SousBloc> resPdf;
    List<CvAvecBlocs> resExcel;

    RestTemplate rt = new RestTemplate();
    rt.getMessageConverters().add(new StringHttpMessageConverter());

    if(extension.equals("xlsx")){
        resExcel = rt.getForObject("http://localhost:8080/readExcel/"+file, List.class, 200);
        return new ResponseEntity<>(resExcel, HttpStatus.OK);
    }
    else if(extension.equals("pdf")){
        resPdf = rt.getForObject("http://localhost:8080/readPdf/"+file, List.class, 200);
        return new ResponseEntity<>(resPdf, HttpStatus.OK);
    }
    else if(extension.equals("docx")){
        resWord = rt.getForObject("http://localhost:8080/readWord/"+file, List.class, 200);
        return new ResponseEntity<>(resWord, HttpStatus.OK);
    }

    return null;

}

有我的阅读词控制器:

@Controller
public class ReadWordController {

    private static String UPLOADED_FOLDER = "C:\\cvsUploades\\";

    @Autowired
    ReadWord readWord;

    @RequestMapping(value="/readWord/{file:.+}" , method =  RequestMethod.GET)
    public ResponseEntity readingWord(@PathVariable("file") String file) throws IOException {

        String path = UPLOADED_FOLDER+file;
        List<SousBloc> sousBlocs = readWord.extract(path);

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

    }

}

这个控制器工作正常并完成工作。

现在有我的阅读Pdf控制器:

@Controller
public class ReadPdfController {

    private static String UPLOADED_FOLDER = "C:\\cvsUploades\\";

    @Autowired
    ReadPdf readPdf;

    @RequestMapping(value="/readPdf/{file:.+}" , method =  RequestMethod.GET)
    public ResponseEntity readingPdf(@PathVariable("file") String file) throws IOException {

        String path = UPLOADED_FOLDER+file;
        List<SousBloc> blocs = readPdf.extract(path);

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

    }

}

它是在阅读字控制器的同一型号上构建的,但它不起作用。在调试中,程序正常工作,直到抛出406错误的return new ResponseEntity<>(blocs, HttpStatus.OK);为空...

你知道为什么吗?

编辑:我尝试了一些奇怪的东西并且它有效...我输入以下代码:

@Controller
public class ReadWordController {

    private static String UPLOADED_FOLDER = "C:\\cvsUploades\\";

    @Autowired
    ReadWord readWord;

    @Autowired
    ReadPdf readPdf;

    @RequestMapping(value="/readWord/{file:.+}" , method =  RequestMethod.GET)
    public ResponseEntity readingWord(@PathVariable("file") String file) throws IOException {

        /*String path = UPLOADED_FOLDER+file;
        List<SousBloc> sousBlocs = readWord.extract(path);

        return  new ResponseEntity<>(sousBlocs, HttpStatus.OK);*/

        String path = "C:\\cvsUploades\\file.pdf";
        List<SousBloc> blocs = readPdf.extract(path);

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

    }

}

0 个答案:

没有答案