Unified server returns Json data types

code: http response status code

status: Response Status

message: response information

data: response data

 

maven introduced json tools

        <!-- json -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

 

HttpResult.java
com.google.gson.Gson Import; 

Import the java.util.HashMap; 
Import java.util.LinkedHashMap; 

/ * * 
 * The results returned from the server 
 * @author suphowe 
 * / 
public  class HttpResult { 

    / * * 
     * Json return type data 
     * @param code HTTP response status codes 
     * @param data return data 
     * @return json data 
     * / 
    public  static String returnJsonData ( int code, data Object) { 
        the HashMap <String, Object> = Result new new the HashMap <String, Object> (); 
        result.put ( " code " , code);
        result.put ( " Message " , HttpResult.CODE_MSG_MAP. GET (code)); 
        result.put ( " Data " , Data);
         return  new new Gson () the toJson (Result);. 
    } 

    / * * 
     * the HTTP response status code and in response to the mapping relationship information 
     * / 
    Private  static a LinkedHashMap <Object, Object> = CODE_MSG_MAP new new a LinkedHashMap <Object, Object> ();
     static { 
        CODE_MSG_MAP.put ( 100 , " the Continue " ); 
        CODE_MSG_MAP.put ( 200 is ," Success" ); 
        CODE_MSG_MAP.put ( 400 , " the Data the Format Error " );          // data format error 
    } 
}

 

Guess you like

Origin www.cnblogs.com/suphowe/p/12517819.html