springboot annotation and GET, POST interface writing_java_ script home � SSI �ļ�ʱ�
java
� SSI �ļ�ʱ�
Home > Software Programming > java > springboot get post interface writing

springboot annotation and GET, POST interface writing

2024-04-02 11:41:31 Author: Cow right knife face

springboot provides @Contrller and @RestController annotations,@Controller returns pages and data and @RestController returns data, this article focuses on springboot annotations and GET, POST interface writing, interested friends one Take a look

I. Comments

springboot provides @Contrller and @RestController.

@Controller: Returns the page and data

@RestController: Returns data

@RestMapping Note: Mainly do path mapping url

value: indicates the path to the requested URL.

method: indicates the HTTP request method.

@RestMapping(value="user", method= RequestMethod.GET)

1.1 GET

parameter-free

@RequestMapping (value="/hello", method= RequestMethod.GET)
    public String hello(String name){
        return "123"+name;
    }

Parameter passing

@RequestMapping (value="/hello", method= RequestMethod.GET)
    public String hello(String name){
        return "123"+name;
    }

Parameter mapping

The @RequestParam annotation represents the parameter mapping and maps the nickname that came in to name

@RequestMapping (value="/hello2", method= RequestMethod.GET)
    public String hello2(@RequestParam(value ="nickname",required = false) String name){
        return "123"+name;
    }

1.2 POST

parameter-free

@RequestMapping(value = "/post1", method = RequestMethod.POST)
    public String post1(){
        return "hello post";
    }

Tape parameter

@RequestMapping(value = "/post2", method = RequestMethod.POST)
    public String post2(String username, String password){
        return username+"-"+password;
    }

Bean encapsulation

@RequestMapping(value = "/post3",method = RequestMethod.POST)
    public String post3(User user){
        System.out.println(user);
        return "post";
    }

json

To add a @RequestBody annotation to the parameter, pass in the same parameter name as the class's private variable

@RequestMapping(value = "/post34",method = RequestMethod.POST)
    public String post4(@RequestBody User user){
        System.out.println(user);
        return "post";
    }

1.3 Errors

To this article about springboot annotations and GET, POST interface writing is introduced to this, more related to springboot get post interface writing content please search script home previous articles or continue to browse the following related articles hope that you will support script home in the future!

Articles you may be interested in:
Read the full article
� SSI �ļ�ʱ�
� SSI �ļ�ʱ�