`

spring mvc(五)Writing Response

 
阅读更多
Response Body

http://localhost:8080/spring_mvc_test/response/annotation
@RequestMapping(value="/response/annotation", method=RequestMethod.GET)
public @ResponseBody String responseBody() {
	String message = "The String ResponseBody";
	return message;
}


ResponseEntity(custom status)
The String ReponseBody with custom status code(403 Forbidden)

@RequestMapping(value="/response/entity/status", method=RequestMethod.GET)
public ResponseEntity<String> responseEntityStatusCode() {
	return new ResponseEntity<String>("the String ResponseBody with custom status code (403 Forbidden)",
				HttpStatus.FORBIDDEN);
}

ResponseEntity(custom headers)
The String ReponseBody with custom header Content-Type=test/plain

@RequestMapping(value="/response/entity/headers", method=RequestMethod.GET)
public ReponseEntity<String> responseEntityCustomHeaders() {
	HttpHeaders headers = new HttpHeaders();
	headers.setContentType(MediaType.TEXT_PLAIN);
	return new ReponseEntity<String>("The String ResponseBody with custom header Content-Type=test/plain",
	headers, HttpStatus.OK);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics