@RequestMapping without parameter in Spring MVC

12,469
  1. With annotation @RequestMapping you can bind request parameters with several way:

    URI Template Patterns, use annotation @PathVariable

    @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
    public String findOwner(@PathVariable String ownerId, Model model) {
        Owner owner = ownerService.findOwner(ownerId);
        model.addAttribute("owner", owner);
        return "displayOwner";
    }
    

    Request Parameters and Header Values

    @Controller
    @RequestMapping("/owners/{ownerId}")
    public class RelativePathUriTemplateController {
    
        @RequestMapping(value = "/pets/{petId}", method = RequestMethod.GET, params = "myParam=myValue")
        public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
            // implementation omitted
        }
    }
    

    use @RequestParam

    @Controller
    @RequestMapping("/pets")
    @SessionAttributes("pet")
    public class EditPetForm {
    
        @RequestMapping(method = RequestMethod.GET)
        public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
            Pet pet = this.clinic.loadPet(petId);
            model.addAttribute("pet", pet);
            return "petForm";
        }
    }
    

    Mapping the request body with the @RequestBody annotation

    @RequestMapping(value = "/something", method = RequestMethod.PUT)
    public void handle(@RequestBody String body, Writer writer) throws IOException {
        writer.write(body);
    }
    
  2. Autowiring

    @Autowired
    private RuleStepService ruleStepService;
    

    The Spring Container has created the bean ruleStepService before. If you need use this bean in your class you only need declare as above and the container will inject the bean into your class . You don't need declare like;

    RuleStepService ruleStepService =new RuleStepService(). 
    

    Container will find the bean name ruleStepService or the bean has type RuleStepService ( based on the strategy in configuration )

Share:
12,469
Umesh Patil
Author by

Umesh Patil

Software Analyst

Updated on June 29, 2022

Comments

  • Umesh Patil
    Umesh Patil about 2 years

    I am trying to understand my project Code while learning Spring MVC.

    In spring @RequestMapping annotation takes parameters. For example,

    @RequestMapping(value="/something", method=RequestMethod.POST)
    @RequestMapping(value="/index.html", method=RequestMethod.GET)
    @RequestMapping("/index")
    @RequestMapping(params="command=GETINFO")
    

    My project uses annotations and it doesn't use any XML for mapping. I have a below structure of Controller.

    @Controller
    public class RuleStepController {
       private static final String ATTRIBUTE_BRANCH = "branch";
        private static final String ATTRIBUTE_EDIT_FORM = "editForm";
     .................
        @Autowired
        private RuleStepService ruleStepService;
        @Autowired
        private PopulationDao populationDao;
    
         @RequestMapping
        public void ruleStepEntForm(Long populationId, ModelMap model) {
         .....
         editForm.setStepEnt(stepDto);
        }
    
    @RequestMapping
        public void ruleStepOrgCount(RuleStepOrgSearchForm searchForm, ModelMap model){
          .......
    model.addAttribute("searchForm", searchForm);
        }
    
    @RequestMapping
        public String ruleStepMgrForm() {
            logger.debug(String.format("ruleStepMgrForm"));
            return "forward:/employee/employeeSearchForm.view?relationshipId=0&roleId=0&formId=stepMgr";
        }
    

    I would like to understand what would be significance of @RequestMapping when it doesn't carry any parameter?

    What is use of @AutoWired ?

  • Raedwald
    Raedwald almost 10 years
    So, given the OP's presented code, which pattern of URIs (GET requests), will be processed by the ruleStepOrgCount method?
  • Ramzan Zafar
    Ramzan Zafar almost 10 years
    A GET method that has no additional mapping is mapped to the root of the controller-mapping itself.
  • Raedwald
    Raedwald almost 10 years
    I know. But in this case the @Controller does not itself have a @RequestMapping. So, what is its URI pattern?