Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestMapping without parameter in Spring MVC

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 ?

like image 708
Umesh Patil Avatar asked Jul 22 '14 11:07

Umesh Patil


People also ask

Is @RequestMapping mandatory?

A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.

What is true about @RequestMapping annotation in Spring MVC?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

Can we use getmapping without RequestMapping?

It is always advised to be specific while declaring @RequestMapping on the controller methods as in most mapping handler classes, @Getmapping is not used. This feature distinguishes the @Getmapping and @RequestMapping annotations. It's an annotation that serves as a shortcut for the @Requestmapping annotation.

What is the default method of RequestMapping?

What is the default request mapping method? If we simply don't specify any request path value in the @RequestMapping annotation of a Controller's method, that method is designated as the default request mapping method for that class.


1 Answers

  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 )

like image 117
Anh Thu Avatar answered Oct 12 '22 14:10

Anh Thu