spring rest

1. The basic principle of Rest (representative state transfer) Rest has nothing to do with rpc. RPC is service-oriented and focuses on behavior and actions, while REST is resource-oriented.

1: Representational, resources can actually be represented in various forms. xml, json, html

2: State When using REST, we pay more attention to the state of the resource

3: REST involves the transfer of resource data, which is transferred from an application to an application in a representational form

 

Summary: rest is to transfer the state of resources from the server to the client in the most appropriate form.

 

2. How does spring support rest: 

1. The controller can handle all http methods, get, put, delete, post 

2. The new pathvariable annotation enables controllers to handle parameterized URLs

3. Spring's form binds the <form:form> tag of the jsp tag library and the new HiddenHttpMethodFilter, making it possible to submit put and delete requests through the html form.

4. By using spring's views and view resolvers, resources can be expressed in various forms. xml, json, atom and rss

5. You can use the new contentnegotiatingviewresolver to select the most suitable client

6. View-based rendering can be annotated with @responsebody

7. The new @responsebody annotation and httpmethodconverter implementation can convert incoming http data into controller processing methods

8. Resttemplate simplifies the use of rest resources by clients.

 

urls and controllers for restful and restless

url and controller for rest less

1、restless的url : http://localhost:8080/Spitter/displaySpittle.htm?id=123

 

2、

@Controller

@RequestMapping(“/displaySpitle.html”)

public class DisplaySpittleController {

    private final SpitterService spitterService ;

     @Inject

      public DisplaySpittleController(SpitterService spitterService){

           this. spitterService = spitterService

      }

      

      @RequestMapping(method = RequestMethod.GET)

      public string show spittle(@RequestParam(“id”) long id , Model model)

      {

            model.addAttribute (spitterService.getSpittleByid (id))

            return  “spitter/view”

       }

}

 

restful url and controller

1、http://localhost:8080/Spitter/spittles/123

 

2、

@Controller

@RequestMapping(“/spittles”)

public class SpittleController {

    private SpitterService spitterService;

    

    @Inject

    public SpittleController(SpitterService spitterService){

        this.spitterService = spitterService;

    }

    

    @RequestMapping(value = “/{id}” method = RequestMethod.GET)

     public String getSpittle(@PathVariable(“id”) long id , Model model){

            model.addAttribute (spitterService.getSpittleById (id));

            return “spittles/view”

     }

}

 

rest action

1. For any given resource, the most common operations are to create, index, update, delete the resource on the server

2. Each http method has two properties, security and idempotency. 

3. http provides multiple methods to manipulate resources

Safe: do not change resource state

Idempotent: one request and multiple requests have the same effect

 

GET method: Perform a read operation. is safe and idempotent.

POST method: Typically used for update and create operations. Not safe, not idempotent.

PUT method: Typically used for update and create operations. insecure, idempotent

DELELE method: delete operation, unsafe, idempotent

OPTIONS method: Requests options available for communication with the server, is safe, and is idempotent

HEAD method: Similar to GET, only returning header information is safe and idempotent

TRACE method: Make the content of the request body safe and idempotent

 

4. Use PUT to update resources

A get request transfers the state of a resource from the server to the client, while put transfers the state of the resource from the client to the server.

 

@RequestMapping(value="/{id}",method=RequestMethod.PUT)

@ResponseStatus(HttpStatus.NO_CONTENT)

public void putSpittle(@PathVariable("id") long id , @Valid Spittle spittle){

             spitterService.saveSpittle(spittle);

 

5. Handling Delete requests: RequestMethod.delete

 

6. Use post to create resources: you can do things that other http methods can't do

When the method's requestmapping has no value, match the value of the class

@RequestMapping(method = RequestMethod.POST)

@ResponseStatus(HttpStatus.CREATED)

public @ResponseBody Spittle createSpittle(@Valid Spittle spittle,BindingResult result,HttpServleResponse response){

         if(result.hasErrors()){

              throw new BindException();

         }

         

         spitterService.saveSpittle(spittle);

         response.setHeader("Location"," /spittles/" + spittle.getId()+"");

         return spittle

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326615654&siteId=291194637