Return only string message from Spring MVC 3 Controller

后端 未结 7 688
死守一世寂寞
死守一世寂寞 2020-11-28 04:50

Can any one tell me how I can return string message from controller?

If i just return a string from a controller method then spring mvc treating it as a jsp view nam

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 05:00

    This is just a note for those who might find this question later, but you don't have to pull in the response to change the content type. Here's an example below to do just that:

    @RequestMapping(method = RequestMethod.GET, value="/controller")
    public ResponseEntity displayUploadedFile()
    {
      HttpHeaders headers = new HttpHeaders();
      String disposition = INLINE;
      String fileName = "";
      headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    
      //Load your attachment here
    
      if (Arrays.equals(Constants.HEADER_BYTES_PDF, contentBytes)) {
        headers.setContentType(MediaType.valueOf("application/pdf"));
        fileName += ".pdf";
      }
    
      if (Arrays.equals(Constants.HEADER_BYTES_TIFF_BIG_ENDIAN, contentBytes)
          || Arrays.equals(Constantsr.HEADER_BYTES_TIFF_LITTLE_ENDIAN, contentBytes)) {
        headers.setContentType(MediaType.valueOf("image/tiff"));
        fileName += ".tif";
      }
    
      if (Arrays.equals(Constants.HEADER_BYTES_JPEG, contentBytes)) {
        headers.setContentType(MediaType.IMAGE_JPEG);
        fileName += ".jpg";
      }
    
      //Handle other types if necessary
    
      headers.add("Content-Disposition", , disposition + ";filename=" + fileName);
      return new ResponseEntity(uploadedBytes, headers, HttpStatus.OK);
    }
    

提交回复
热议问题