Spring Rest控制器检索图像 - 客户端发送的请求在语法上是不正确的

时间:2018-02-21 22:05:57

标签: java spring rest

我正在尝试从Spring RestController中检索图像 我有两个方法getPictureById2和getPictureById
getPictureById2只是我添加的测试方法

我有春季版:     4.0.5.RELEASE

getPictureById2 - (测试方法)工作得非常好并打印:

{"YES, ITS ":"working Wed Feb 21 16:56:31 EST 2018!"}

对于另一个方法getPictureById,我无法打破它或看到它的任何打印,这是我得到的错误:

HTTP状态400 - 输入状态报告 description客户端发送的请求在语法上是不正确的。 Apache Tomcat / 8.0.15

我做错了什么? 这是控制器代码:

@RestController
@RequestMapping("/image")
public class ImageApi{

    @RequestMapping("/getPictureById2/{pictureId}")
     public ResponseEntity<String>  getPictureById2(@PathVariable("pictureId") String pictureId){
        return Helper.getRsponse(HttpStatus.INTERNAL_SERVER_ERROR,"{\"YES, ITS \":\"working "+new Date()+"!\"}");
    }

    @RequestMapping("/getPictureById/{pictureId}")
     public ResponseEntity<InputStreamResource>  getPictureById(@PathVariable("pictureId") String pictureId) {

        String fileName = pictureId +".jpg";


            try {
                ClassPathResource picFile = new ClassPathResource(Helper.IMG_PATH+  fileName);
                  HttpHeaders headers = new HttpHeaders();
                  headers.setContentType(MediaType.IMAGE_JPEG);             
                  headers.setContentLength(picFile.contentLength());
                  ResponseEntity<InputStreamResource> response = new ResponseEntity<InputStreamResource>(
                                    new InputStreamResource(picFile.getInputStream()), headers, HttpStatus.OK);
                  return response;
            } catch (IOException e) {
                logger.error("getPictureById error " + e.getMessage(),e) ;
            }

    return null;
    }

0 个答案:

没有答案