MediaType is the interface test method of application/x-www-form-urlencoded

Source: Internet
Author: User

First look at the interface:

  @POST   @Path /span> (/deletebyid  ")  //  @Consumes (Mediatype.application_json)   @Consumes  (mediatype.)  public  Resultmodel Deletebyprimarykey ( @FormParam  ("id " Span style= "COLOR: #000000" >) Integer personId) { int  count = Personservice.deletebyprimarykey (personId);     return   Resultmodel.ok (count); }

This interface restricts mediatype to application_form_urlencoded, so the test HTTP request is as follows:

Note 2 points:

(1) Content-type must be application/x-www-form-urlencoded, this and the interface above the annotation application_form_urlencoded is corresponding.

Otherwise, an error will be reported, such as the HTTP request in the Content-type to Application/json, which obviously does not match the media type invited on the interface, will report the following errors:

(2) because the parameters received on the interface use @formprame annotations, it is required to send an HTTP request when the parameter transfer must be form form, that is, id=2 such a way, otherwise the following error will be reported:

[Summary]: If the HTTP request is sent when the parameters in the body form forms, such as id=2, the request for the interface mediatype to use application_form_urlencoded, The @formparam ("id") annotation is used on the parameters of the interface, and the "id" in the annotation is consistent with the name of the parameter passed when the HTTP request is sent.

If the parameter is "Id=2&name= Zhang San" when sending an HTTP request, the parameters of the background interface should correspond to the following:

    @POST    @Path ("/deletebyid")    //@Consumes (Mediatype.application_json)      @Consumes (mediatype.application_form_urlencoded)    public Resultmodel Deletebyprimarykey (@FormParam ("id") Integer personId, @FormParam ("name") String name) {        int count = personservice.deletebyprimarykey (personId);         return Resultmodel.ok (count);    }

If you want the body part of the HTTP request to be in JSON format, for example:

Then, the corresponding interface should be the following form:

    @POST    @Path ("/deletebyid")    @Consumes (Mediatype.application_json)    //  @Consumes (mediatype.application_form_urlencoded)public      Resultmodel Deletebyprimarykey (person person) {        int count = Personservice.deletebyprimarykey ( Person.getpersonid ());         return Resultmodel.ok (count);    }

Note: When sending an HTTP request, the parameter "PersonId" must be consistent with the name of the attribute personId in the person in the interface.

The person class is as follows:

Importjava.util.Date; Public classPerson {PrivateInteger personId; PrivateString PersonName; PrivateInteger Gender; PrivateString personaddr; PrivateDate birthday;  PublicInteger Getpersonid () {returnpersonId; }     Public voidSetpersonid (Integer personId) { This. PersonId =personId; }     PublicString Getpersonname () {returnPersonName; }     Public voidsetpersonname (String personname) { This. PersonName = PersonName = =NULL?NULL: Personname.trim (); }     PublicInteger Getgender () {returngender; }     Public voidSetgender (Integer gender) { This. Gender =gender; }     PublicString getpersonaddr () {returnpersonaddr; }     Public voidsetpersonaddr (String personaddr) { This. personaddr = Personaddr = =NULL?NULL: Personaddr.trim (); }     PublicDate Getbirthday () {returnbirthday; }     Public voidsetbirthday (Date birthday) { This. Birthday =birthday; }}

MediaType is the interface test method of application/x-www-form-urlencoded

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.