Configuring the response content type without using HttpServletResponse

How can I get HttpServletResponse objectin a method in my spring controller so that my application remains loosely coupled to the Http API?

Thank...

Edit: Actually, I want to set the ContentType of the HttpServletResponse object in my controller. Does spring provide any way to do this without getting the HttpServletResponse object as an argument in the controller method?

+5
source share
3 answers

I see two options:

If the type of content you want is static, you can add it to @RequestMapping, for example.

@RequestMapping(value="...", produces="text/plain")

, HTTP- Accept. . 16.3.2.5 .

ResponseEntity,

@RequestMapping("/something")
public ResponseEntity<String> handle() {
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.setContentType(new MediaType("text", "plain"));
  return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}

MediaType mime, , . MediaType.TEXT_PLAIN.

. 16.3.3.6 HttpEntity<?>

+8

, .

@RequestMapping( value="/test", method = RequestMethod.GET )
public void test( HttpServletResponse response ) { ... }
+1
0

All Articles