Several ways of "SPRINGMVC" Value &&postman interface test

Source: Internet
Author: User

Recently in the use of Postman Test Postman Interface, for the SPRINGMVC value this piece, tested several common ways, summed up. The use of the Postman tool has also been added to the understanding. Postman testing is great, with tools, testing interfaces, and inefficient.


One, single parameter passing


[Email protected] Annotations

[Java]View PlainCopyPrint?
  1. <span style= "font-family: ' kaiti_gb2312 '; font-size:18px;" > /**
  2. * Test single parameter @requestbody
  3.      */   
  4. @CrossOrigin   
  5. @RequestMapping (value = {"/inserttestparamsrequest"}, method = Requestmethod.get)
  6. @ResponseBody   
  7. Public void inserttestparamsrequest (@RequestBody String name, @ Requestbody String age) {
  8. System.out.println ("name=====" + name);
  9. System.out.println ("age=====" + age);
  10. }
  11. </span>
   /**     * Test a single parameter @requestbody *    /@CrossOrigin @RequestMapping (value = {"/inserttestparamsrequest"}, method = Requestmethod.get)    @ResponseBody public    void Inserttestparamsrequest (@RequestBody String name, @ Requestbody String age) {        System.out.println ("name=====" + name);        System.out.println ("age=====" + age);    }

Test Request Path


[Email protected]

Commonly used to handle a simple type of binding, a string obtained through Request.getparameter () can be converted directly to a simple type of case (;
The note has two properties: value, required; value is used to specify the ID name to pass in the value, required is used to indicate whether the parameter must be bound;

[Java]View PlainCopyPrint?
  1. <span style= "font-family: ' kaiti_gb2312 '; font-size:18px;" > /**
  2. * Test single parameter @requestparam
  3.      */   
  4. @CrossOrigin   
  5. @RequestMapping (value = {"/inserttestparams"}, method = Requestmethod.get)
  6. @ResponseBody   
  7. Public  void inserttestparams (httpservletrequest request, @RequestParam String name, @RequestParam string age) {
  8. System.out.println ("name=====" + name);
  9. System.out.println ("age=====" + age);
  10. }</span>
/**     * Test single parameter @requestparam     *    /@CrossOrigin @RequestMapping (value = {"/inserttestparams"}, Method = Requestmethod.get)    @ResponseBody public    void Inserttestparams (HttpServletRequest request, @RequestParam String name, @RequestParam string age) {        System.out.println ("name=====" + name);        System.out.println ("age=====" + age);    }

Request Path:


[Email protected] Annotations

The path is resultful style and the parameter is used as the request path.

When using the @requestmapping URI template style mapping, which is Url/{paramid}, the Paramid can bind the value passed to the method by @Pathvariable annotations.

[Java]View PlainCopyPrint?
  1. <span style= "font-family: ' kaiti_gb2312 '; font-size:18px;" > /**
  2. * Test single parameter @pathvariable
  3.      */   
  4. @CrossOrigin   
  5. @RequestMapping (value = {"/inserttest/{name}/{age}"}, method = Requestmethod.get)
  6. @ResponseBody   
  7. Public void inserttestpathveriable (httpservletrequest request, @PathVariable( "name") string name, @PathVariable string age) {
  8. System.out.println ("name=====" + name);
  9. System.out.println ("age=====" + age);
  10. }</span>
   /**     * Test single parameter @pathvariable     *    /@CrossOrigin @RequestMapping (value = {"/inserttest/{name}/{age}"}, method = Requestmethod.get)    @ResponseBody public    void inserttestpathveriable (HttpServletRequest request, @ Pathvariable ("name") string name, @PathVariable string age) {        System.out.println ("name=====" + name);        System.out.println ("age=====" + age);    }

The above code binds the value of the variable name in the URI template and the value of age to the parameter of the method. If the variable names in the method parameter names and URI template that need to be bound are inconsistent, you need to specify the name in the URI template in @pathvariable ("name").


Second, pass the Pojo object


[Email protected] Annotations

[Java]View PlainCopyPrint?
  1. <span style= "font-family: ' kaiti_gb2312 '; font-size:18px;" > / * Test Add entity * /   
  2. @CrossOrigin   
  3. @RequestMapping (value = {"/insertentitytest"}, method = Requestmethod.post)
  4. @ResponseBody   
  5. Public  void insertentitytest (@RequestBody curriculumscheduleentity curriculumscheduleentity) {
  6. System.out.println ("name=====" + curriculumscheduleentity.getclassid ());
  7. System.out.println ("age=====" + curriculumscheduleentity.getteachclassid ());
  8. }</span>
    /* Test Add Entity    *    /@CrossOrigin @RequestMapping (value = {"/insertentitytest"}, method = Requestmethod.post)    @ Responsebody public    void Insertentitytest (@RequestBody curriculumscheduleentity curriculumscheduleentity) {        System.out.println ("name=====" + curriculumscheduleentity.getclassid ());        System.out.println ("age=====" + curriculumscheduleentity.getteachclassid ());    }

Postman Test with JSON format



2. Write entities directly

[Java]View PlainCopyPrint?
  1. <span style= "font-family: ' kaiti_gb2312 '; font-size:18px;" > / * Test Add entity * /   
  2. @CrossOrigin   
  3. @RequestMapping (value = {"/inserttest"}, method = Requestmethod.post)
  4. @ResponseBody   
  5. Public void inserttest (curriculumscheduleentity curriculumscheduleentity) {
  6. System.out.println ("name=====" + curriculumscheduleentity.getclassid ());
  7. System.out.println ("age=====" + curriculumscheduleentity.getweekid ());
  8. }</span>
    /* Test Add Entity    *    /@CrossOrigin @RequestMapping (value = {"/inserttest"}, method = Requestmethod.post)    @ Responsebody public    void Inserttest (curriculumscheduleentity curriculumscheduleentity) {        System.out.println ("name=====" + curriculumscheduleentity.getclassid ());        System.out.println ("age=====" + curriculumscheduleentity.getweekid ());    }

Form form Test [HTML]View PlainCopyPrint?
  1. < span style="font-family: ' kaiti_gb2312 '; font-size:18px;" > < Div >   
  2. < form action="/curriculumschedule/inserttest" method ="POST">
  3. ClassId:<input name="classId">< br >
  4. Teachclassid:<input name="weekid"> <br >
  5. < input type= " submit" value="commit">   
  6. </ form >   
  7. </ Div > </ span >   
<div>    <form action= "/curriculumschedule/inserttest" method= "POST" >         classId: <input  Name= "ClassId" ><br>        teachclassid:<input   name= "Weekid" ><br>        <input type= " Submit "value=" >    </form></div>

Postman Test format


Iii. postman Test List type parameters

Take list<string> as an example, test the bulk Delete method, the parameter is list<string>. Write this actually no technology, but at noon in the Test list interface, with postman test, format consistent write wrong, do not know how to test with postman. So it took a little time to record and think about the process of the tool's execution.

Controller method


3. Reference connection

Http://www.cnblogs.com/sandyliu1999/p/4802706.html



"SPRINGMVC" several ways to pass the value &&postman interface test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.