spring mvc(五)Writing Response
[color=blue]Response Body[/color] http://localhost:8080/spring_mvc_test/response/annotation [code="java"]@RequestMapping(value="/response/annotation", method=RequestMethod.GET) public @ResponseBody String responseBody() { String message = "The String ResponseBody"; return message; }[/code] [color=blue]ResponseEntity(custom status) The String ReponseBody with custom status code(403 Forbidden)[/color] [code="java"]@RequestMapping(value="/response/entity/status", method=RequestMethod.GET) public ResponseEntity responseEntityStatusCode() { return new ResponseEntity("the String ResponseBody with custom status code (403 Forbidden)", HttpStatus.FORBIDDEN); }[/code] [color=blue]ResponseEntity(custom headers) The String ReponseBody with custom header Content-Type=test/plain[/color] [code="java"]@RequestMapping(value="/response/entity/headers", method=RequestMethod.GET) public ReponseEntity responseEntityCustomHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); return new ReponseEntity("The String ResponseBody with custom header Content-Type=test/plain", headers, HttpStatus.OK); }[/code]