Ma Ying-jeou spring rest

 

Idempotent

PUT

Initial state: 0

Status Review: 1 * N

Final state: 1

 

DELETE

Initial state: 1

Status Review: 0 * N

Final state: 0

 

Non-idempotent

POST

Initial state: 1

Status Review: 1 + 1 = 2

N revision: 1+ N = N + 1

Final state: N + 1

 

Idempotent / non-idempotent achieved depends on the server, in this way, a contract

 

# Self-describing messages

 

> Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8

 

First Priority: text / html -> application / xhtml + xml -> application / xml

Second Priority: image / webp -> image / apng

 

"Spring Boot programming ideas"

 

Learning source path:

@EnableWebMvc

DelegatingWebMvcConfiguration

​ WebMvcConfigurationSupport#addDefaultHttpMessageConverters

 

 

 

 

HTTP messages from all processors in the description of MessageConverters (Type: `HttpMessageConverter`), this set will be transmitted to RequestMappingHandlerAdapter, ultimately control write.

 

of MessageConverters, wherein the self-describing message contains many types of processing, such as JSON, XML, TEXT, etc.

 

In application / json Case, Spring Boot Jackson2 using default serialization, wherein the media type: application / json, its processing based MappingJackson2HttpMessageConverter, provided two methods:

1. Read read *: Bean converted to the corresponding HTTP request content
2. Write write *: text into a corresponding sequence as a response content by Bean

 

Question: Why is the first time JSON, XML and later added a how-dependent, he is a XML output within

Answer: Spring Boot application did not increase the default XML processor (HttpMessageConverter) to achieve, so in the end adopt in rotation way to try to take it one by one canWrite (POJO), if it returns true, explain the POJO objects can be serialized, then just to deal with Jackson 2 then Jackson output.

 

Problem: When Accept request header has not been developed, or why JSON to handle

Answer: This depends on the insertion order messageConverters.

 

Question: is the default priority right can modify it

Answer: can be adjusted, the adjustment method by extendMessageConverters

 

### extended from the description of the message

Person

JSON format (application / json)

```json
{
"id":1,
"name":"小马哥"
}
```

XML format (application / xml)

```xml
<Person>
<id>1</id>
<name>小马哥</name>
</Person>
```

Properties 格式(application/properties+person)

(Need to expand)

Properties `
person.id. 1 =
PERSON.NAME Mr Ma =
`

 

1. To achieve AbstractHttpMessageConverter abstract class
1. The method of the Supports: whether the current POJO type supports
2. readInternal Method: reading content of the HTTP request, and is converted to the corresponding POJO objects (JSON by transforming into Properties content)
3. writeInternal method: POJO text content into the content sequence (Properties format), the HTTP response to the final output (JSON content by conversion into Properties)

* @RequestMappng corresponding to the request header consumes "the Type-the Content"
* @RequestMappng produces a corresponding request header in the "Accept"

 

HttpMessageConverter execution logic:

* Read: Are you trying to be able to read, canRead methods to try, returns true if the next step to perform the Read
* writes: Are you trying to be able to write, canWrite methods to try, returns true if next execution write

 

Guess you like

Origin www.cnblogs.com/q1359720840/p/11164063.html