@PathVariable annotation description and its usage

Hits: 0

@PathVariable map URL binding placeholder

  • URLs with placeholders are a new feature in Spring 3.0, which is a milestone in the development of SpringMVC toward REST goals.
  • The placeholder parameters in the URL can be bound to the input parameters of the controller processing method through @PathVariable: the {xxx} placeholders in the URL can be bound to the input parameters of the operation method through @PathVariable(“xxx”) .

// Send message 
    @GetMapping ( "/kafka/normal/{message}" )
     public  void  sendMessage1 (@PathVariable( "message" ) String normalMessage) {
        kafkaTemplate.send("testTopic",normalMessage);
    }

  1. If the method parameter name is the same as the variable name in the url to be bound, it can be abbreviated as follows:

// Send message 
    @GetMapping ( "/kafka/normal/{message}" )
     public  void  sendMessage1 (@PathVariable String message) {
        kafkaTemplate.send("testTopic",message);
    }

@PathVariable cannot be omitted, otherwise the parameters in the placeholder will not be obtained. You can try it yourself by breaking the point. After removing it, the parameters we receive are null

  1. If the method parameter name is inconsistent with the variable name in the url to be bound, it cannot be abbreviated:

// Send message 
    @GetMapping ( "/kafka/normal/{message}" )
     public  void  sendMessage1 (@PathVariable( "message" ) String normalMessage) {
        kafkaTemplate.send("testTopic",normalMessage);
    }

You may also like...

Leave a Reply

Your email address will not be published.