Professional Documents
Culture Documents
@RequestMapping("/echo/json")
public @ResponseBody SomeObject readJson() {}
Click to edit Master title style
HttpMessageConverters
Howto
Click does Spring text
edit Master MVC know how to turn a JSON string into
styles
SomeObject,
Second level or vice-versa?
HttpMessageConverters
Third level
These are responsible
Fourth level for converting a request body to a
certain type, or a certain
Fifth level type into a response body
Spring MVC figures out which converter to use based on
Accept and Content-Type headers, and the Java type
Your Accept and Content-Type headers DONT have to
match. For example, you can send in JSON and ask for XML
back
Click to edit Master title style
HttpMessageConverters
A number
Click of HttpMessageConverters
to edit Master text styles are already provided
You can define
Second level your own
You dont
Thirdspecify
level which ones are used to convert
request/response bodies they are selected based on the
Fourth level
Content-Type/Accept
Fifth level headers
Click to
MIME edit Master title style
Types
HttpMessageConverters
Click to edit Master text stylesmake heavy use of MIME types
These are level
Second the value for Accept and Content-Type headers
Two-part identifier
Third level for content formats
First part is thelevel
Fourth type. ie, application
Second part is the
Fifth levelsub-type. ie, json
application/json
Click to edit Master title style
StringHttpMessageConverter
Reads
Click andMaster
to edit writestext
Strings.
styles
Reads
Secondtext/*
level
Writes text/plain
Third level
Fourth level
Fifth level
Click to edit Master title style
StringHttpMessageConverter
@RequestMapping("/echo/string")
Click
public to edit MasterString
@ResponseBody text styles
echoString(@RequestBody String input) {
return
Second Your
level Text Was: + input;
}
Third level
POST /echo/string HTTP/1.1 REQUEST
Fourth level
Accept: text/plain
Content-Type: text/plain
Fifth level
Hello!
HTTP/1.1 200 OK RESPONSE
Content-Type: text/plain
Content-Length: 17
<thing><name>SPENCER</name><age>25</age></thing>
Click to edit Master title style
ByteArrayHttpMessageConverter
Click to edit Master text styles
Can read/write
Second level byte arrays (useful for dealing with binary
data, such
Third as images)
level
Reads */* Fourth level
Writes application/octet-stream
Fifth level
Click to edit Master title style
ByteArrayHttpMessageConverter
@RequestMapping("/echo/string")
Click to edit Master text styles
public @ResponseBody String echoString(@RequestBody byte[] input) {
return
Second new
levelString(input);
}
Third level
POST /echo/string HTTP/1.1
Fourth level REQUEST
Accept: text/plain
Fifth level
Content-Type: text/plain
Hello!
Hello!
Click1to edit Master title style
Lab
Create
Click a handler
to edit Master textthat takes a request body and echoes it
styles
back.
Second level
Create a handler
Third level that takes a request body, creates an
object with it, and
Fourth returns it as JSON.
level
Create a handler Fifth that
level takes an XML input and echoes it back
as JSON.
Test all of these with your HttpClient
Click to
Other editof
parts Master titleMessage
the HTTP style
Whattoifedit
Click youMaster
needtext
to get/set
styles headers?
Or set the level
Second status code?
Third level
@RequestMapping("/echo/string")
public @ResponseBody
Fourth String
level echoString(@RequestBody String input,
HttpServletRequest request,
Fifth level HttpServletResponse response) {
String requestType = request.getHeader(Content-Type);
response.setHeader(Content-Type, text/plain);
response.setStatus(200);
return input
}
Click to edit Master title style
@ResponseStatus
There
Click toisedit
a convenient way to set what the default status for a
Master text styles
particular
Second levelhandler should be
@ResponseStatus
Third level
Fourth level
@RequestMapping("/create")
@ResponseStatus(HttpStatus.CREATED)
Fifth level // CREATED == 201
public void echoString(String input) {
}
Click to edit Master title style
HttpEntity
Convenience
Click class
to edit Master textfor dealing with bodies, headers, and
styles
status
Second level
Converts messages
Third level with HttpMessageConverters
@RequestMapping("/image/upload")
Fourth level
public ResponseEntity<String> upload(HttpEntity<byte[]> rEntity) {
Fifth level
String t = rEntity.getHeaders().getFirst(Content-Type);
byte[] data = rEntity.getBody();
// Save the file
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(Location, /image/1);
@ExceptionHandler(DataAccessException.class)
public @ResponseBody String handleDataAccessError(DataAccessException ex) {
return ex.getMessage();
}
Click to edit Master title style
@ResponseStatus
We saw
Click this
to edit annotation
Master earlier
text styles
Itcan alsolevel
Second be placed on Exception classes or
@ExeptionHandler
Third level methods to return a specific status code for
a particular exception
Fourth level
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
Fifth level
@ExceptionHandler(DataAccessException.class)
public void handleDataAccessError(DataAccessException ex) {}