Home > Software design >  Why @PostMapping redirect to @GetMapping?
Why @PostMapping redirect to @GetMapping?

Time:01-20

@GetMapping("/{id}")
    public ModelAndView getPersonById(@PathVariable("id") String id) {

        ModelAndView modelAndView = new ModelAndView();
        Person person = personCache.getById(id);

        modelAndView.setViewName("/author.jsp");
        modelAndView.addObject("id", person.getId());
        modelAndView.addObject("lastName", person.getLastName());
        modelAndView.addObject("firstName", person.getFirstName());
        modelAndView.addObject("secondName", person.getSecondName());
        modelAndView.addObject("phone", person.getPhone());
        modelAndView.addObject("hobby", person.getHobby());
        modelAndView.addObject("bitBucketUrl", person.getBitBucketUrl());
        return modelAndView;
    }

@PostMapping("/create")
    public ModelAndView create(@RequestParam("id") String id, @RequestParam("lastName") String lastName,
                               @RequestParam("firstName") String firstName, @RequestParam("secondName") String secondName,
                               @RequestParam("phone") String phone, @RequestParam("hobby") String hobby,
                               @RequestParam("bitBucketUrl") String bitBucketUrl) {

        ModelAndView modelAndView = new ModelAndView();
        personCache.create(Person.builder()
                .setId(id)
                .setFirstName(firstName)
                .setLastName(lastName)
                .setSecondName(secondName)
                .setPhone(phone)
                .setHobby(hobby)
                .setBitBucketUrl(bitBucketUrl)
                .build());

        modelAndView.setViewName("/create.jsp");
        modelAndView.addObject("id",id);
        modelAndView.addObject("lastName", lastName);
        modelAndView.addObject("firstName", firstName);
        modelAndView.addObject("secondName", secondName);
        modelAndView.addObject("phone", phone);
        modelAndView.addObject("hobby", hobby);
        modelAndView.addObject("bitBucketUrl", bitBucketUrl);
        return modelAndView;
    }

I'm at studying the spring boot app. I want to create an object Person on the page with the URL "localhost:8080/author/create". So, when I type into the browser "localhost:8080/author/create", I'm waiting that my app to go to @PostMapping, but the app redirects to @GetMapping with URL "localhost:8080/author/create". Because "id" with value "create" doesn't exist, I catch a NullPointerExecption in this line:

modelAndView.addObject("id", person.getId());

Error in browser:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
There was an unexpected error (type=Internal Server Error, status=500).

How can I realize "create" page?

CodePudding user response:

Browser address bar sends GET requests. You can use smth like Postman to send requests which you need.

CodePudding user response:

By default browser does get-requests only, so create another controller method with get-mapping and add person object to the model so that you can set values in the view page and when you press submit give post-mapping so that you can assign values that are entered in view page to the person object.

  •  Tags:  
  • Related