Java Interface Definition specification, excerpt from Xiaofeng light column __java

Source: Internet
Author: User

1. Return format is not uniform

The same interface, sometimes returning an array, sometimes returning a single, successful return of the object, failure to return the error message string. There is a system integration in the work that defines the interface, really spicy eyes. On this code, the return type is map,json,object and should not be. In actual work, we will define a unified format, that is, Resultbean, pagination has another Pageresultbean

Error Example:

Return MAP is not readable, try not to
@postmapping ("/delete") public
  map<string, object> Delete (long id, String lang) {

  }

  Successfully returned Boolean, failed to return String, bogey
  @postmapping ("/delete") public
  Object Delete (long id, String lang) {
    try {
      Boolean result = Configservice.delete (IDs, local);
      return result;
    } catch (Exception e) {
      log.error (e);
      return e.tostring ();
    }
  

2. No consideration of failure

Initially only consider the success of the scene, and so on behind the test found that there are errors, how to do, change the interface Bai, both front and back of the station are changed, costly and useless.

Error Example:

Do not return any data, do not consider failure scenarios, easy to rework
@postmapping ("/update") public
  void update (long id, XXX) {

  }

3. Occurrence and business-independent input parameters

such as the Lang language, the current user information should not appear inside the parameter, should be obtained from the current session. Later, Threadlocal will say how to remove. This is a serious problem, in addition to the poor readability of the code, especially when the parameters present user information.

Error Example:

(Current user deletes data) parameter appears Lang and userid, especially the UserID, Bogey
@postmapping ("/delete") public
  map<string, object> Delete ( Long ID, String lang, string userId) {

  }

4. Complex input parameters are present

In general, a parameter such as a JSON string is not allowed, which is extremely readable. The corresponding bean should be defined.

Error Example:

Parameters appear in JSON format, not readable, code ugly
@postmapping ("/update") public
  map<string, object> update (long ID, String JSONSTR) {

  }

5. Did not return the data that should be returned

For example, the new interface should normally return the ID identification of the newer object, which requires programming experience. It is not appropriate for a novice to define a time when the foreground does not return data or only returns true because it is not used. If someone else's business, you should return or should return.

Error Example:

Conventionally, a new object should be returned with information that returns only Boolean easy to cause rework
@postmapping ("/add") Public
  boolean Add (XXX) {
    //xxx
    return Configservice.add ();
  }


Attached Resultbean, without any technical content:

@data

  public class Resultbean<t> implements Serializable {private static final long serialversionuid = 1L;

  public static final int SUCCESS = 0;

  public static final int FAIL = 1;

  public static final int no_permission = 2;

  Private String msg = "Success";

  private int code = SUCCESS;

  Private T data;
  Public Resultbean () {super ();
    Public Resultbean (T data) {super ();
  This.data = data;
    Public Resultbean (Throwable e) {super ();
    This.msg = E.tostring ();
  This.code = FAIL; }
}

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.