JavaLib | Result unifies your API (2)

Keywords: Spring Lombok JSON

Introduction

JavaLib has implemented the Result module for a long time, and I've been using it, but that's for the public API interface. If the back-end developers write the API interface to the front-end like that, it's very troublesome, so they decided to rewrite it early. I also refer to a lot of people. Looking at the Spring Builder mode, it's a bit confusing, and setting calculation is a failure.

outline

Let's see what it looks like.

public class App {

    public static void main(String[] args) {

        // Success, no data, custom return code and description
        Result result1 = ResultResponseUtil.ok().code(0).msg("Success");
        // Success, return data, custom return code and description
        Result result2 = ResultResponseUtil.ok().code(0).msg("Success").data("data");
        // Success, no data, store return code in enum mode
        Result result3 = ResultResponseUtil.ok().status(DefaultReturnCode.SUCCESS);
        // Success, return data, store return code in enum mode
        Result result4 = ResultResponseUtil.ok().status(DefaultReturnCode.SUCCESS).data("data");

        // Failed, no data, custom return code and description
        Result result5 = ResultResponseUtil.error().code(-1).msg("Error");
        // Failure, return data, custom return code and description
        Result result6 = ResultResponseUtil.error().code(-1).msg("Error").data("data");
        // Failed, no data, store return code in enum mode
        Result result7 = ResultResponseUtil.error().status(DefaultReturnCode.ERROR_INIT);
        // Failed, return data, store return code in enum mode
        Result result8 = ResultResponseUtil.error().status(DefaultReturnCode.ERROR_INIT).data("data");

        Console.info(result1);
        Console.info(result2);
        Console.info(result3);
        Console.info(result4);
        Console.info(result5);
        Console.info(result6);
        Console.info(result7);
        Console.info(result8);
        Console.info();
        Console.info(new Gson().toJson(result1));
        Console.info(new Gson().toJson(result2));
        Console.info(new Gson().toJson(result3));
        Console.info(new Gson().toJson(result4));
        Console.info(new Gson().toJson(result5));
        Console.info(new Gson().toJson(result6));
        Console.info(new Gson().toJson(result7));
        Console.info(new Gson().toJson(result8));
    }
}

Print results:

DefaultResult{code=0 success=true msg=Success data=null}
DefaultResult{code=0 success=true msg=Success data=data}
DefaultResult{code=0 success=true msg=Success data=null}
DefaultResult{code=0 success=true msg=Success data=data}
DefaultResult{code=-1 success=false msg=Error data=null}
DefaultResult{code=-1 success=false msg=Error data=data}
DefaultResult{code=-1 success=false msg=init... data=null}
DefaultResult{code=-1 success=false msg=init... data=data}

{"success":true,"code":0,"msg":"Success"}
{"success":true,"code":0,"msg":"Success","data":"data"}
{"success":true,"code":0,"msg":"Success"}
{"success":true,"code":0,"msg":"Success","data":"data"}
{"success":false,"code":-1,"msg":"Error"}
{"success":false,"code":-1,"msg":"Error","data":"data"}
{"success":false,"code":-1,"msg":"init..."}
{"success":false,"code":-1,"msg":"init...","data":"data"}

Use

import com.fengwenyi.javalib.result.DefaultReturnCode;
import com.fengwenyi.javalib.result.Result;
import com.fengwenyi.javalib.result.ResultResponseUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class ApiResultApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiResultApplication.class, args);
    }

    /**
     * Get all user data
     * @return (json)
     */
    @GetMapping("/getUsers")
    public Result getUsers() {

        User user1 = new User().setName("Feng Wen Yi").setAge(26).setGender("male");
        User user2 = new User().setName("Zhang San").setAge(25).setGender("female");
        User user3 = new User().setName("Li Si").setAge(23).setGender("secrecy");
        User user4 = new User().setName("Wang Wu").setAge(20).setGender("Unknown");

        User [] users = {user1, user2, user3, user4};

        return ResultResponseUtil.ok().status(DefaultReturnCode.SUCCESS).data(users);

    }
}

@Data
@Accessors(chain = true)
class User {

    // Full name
    private String name;
    // Age
    private Integer age;
    // Gender
    private String gender;


}

Results (formatted):

{
    "code":0,
    "msg":"Success",
    "data":[
        {
            "name":"Feng Wen Yi",
            "age":26,
            "gender":"male"
        },
        {
            "name":"Zhang San",
            "age":25,
            "gender":"female"
        },
        {
            "name":"Li Si",
            "age":23,
            "gender":"secrecy"
        },
        {
            "name":"Wang Wu",
            "age":20,
            "gender":"Unknown"
        }
    ],
    "success":true
}

There is a problem in this way, which is also found in the previous version. Let's modify the following code:

//return ResultResponseUtil.ok().status(DefaultReturnCode.SUCCESS).data(users);
return ResultResponseUtil.ok().code(0).data(users);

See the result:

API return value is null. I'm sure you have encountered this problem. How can we avoid this problem? There are many solutions on the Internet. What I want to say is that we should write according to the rules and write completely.

Let's look at another approach:

    @GetMapping("/getUsers2")
    public String getUsers2() {

        User user1 = new User().setName("Feng Wen Yi").setAge(26).setGender("male");
        User user2 = new User().setName("Zhang San").setAge(25).setGender("female");
        User user3 = new User().setName("Li Si").setAge(23).setGender("secrecy");
        User user4 = new User().setName("Wang Wu").setAge(20).setGender("Unknown");

        User [] users = {user1, user2, user3, user4};

        Result result = ResultResponseUtil
                .ok()
                .status(DefaultReturnCode.SUCCESS)
                .data(users);
        
        return new Gson().toJson(result);

    }

When writing public interfaces, we must pay attention to avoid all unnecessary.

data

[1] JavaLib

[2] JavaLib Result module test code

[3] Encapsulation of spring MVC return results

[4] JavaLib | result module

Posted by willeh_ on Tue, 31 Dec 2019 10:03:47 -0800