Return only string message from Spring MVC 3 Controller

后端 未结 7 636
死守一世寂寞
死守一世寂寞 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<byte[]> 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<byte[]>(uploadedBytes, headers, HttpStatus.OK);
    }
    
    0 讨论(0)
  • 2020-11-28 05:02

    Although, @Tomasz is absolutely right there is another way:

    @RequestMapping(value="/controller", method=GET)
    public void foo(HttpServletResponse res) {
        try {       
            PrintWriter out = res.getWriter();
            out.println("Hello, world!");
            out.close();
        } catch (IOException ex) { 
            ...
        }
    }
    

    but the first method is preferable. You can use this method if you want to return response with custom content type or return binary type (file, etc...);

    0 讨论(0)
  • 2020-11-28 05:03

    For outputing String as text/plain use:

    @RequestMapping(value="/foo", method=RequestMethod.GET, produces="text/plain")
    @ResponseBody
    public String foo() {
        return "bar";
    }
    
    0 讨论(0)
  • 2020-11-28 05:06

    With Spring 4, if your Controller is annotated with @RestController instead of @Controller, you don't need the @ResponseBody annotation.

    The code would be

    @RestController
    public class FooController {
    
       @RequestMapping(value="/controller", method=GET)
       public String foo() {
          return "Response!";
       }
    
    }
    

    You can find the Javadoc for @RestController here

    0 讨论(0)
  • 2020-11-28 05:09

    What about:

    PrintWriter out = response.getWriter();
    out.println("THE_STRING_TO_SEND_AS_RESPONSE");
    return null;
    

    This woks for me.

    0 讨论(0)
  • 2020-11-28 05:09

    Simplest solution:

    Just add quotes, I really don't know why it's not auto-implemented by Spring boot when response type defined as application/json, but it works great.

    @PostMapping("/create")
    public String foo()
    {
        String result = "something"
        return "\"" + result + "\"";
    }
    
    0 讨论(0)
提交回复
热议问题