As those of you who studied the previous chapter may have noticed, the format returned by interfaces is pretty much fixed, for example

{
	"code": 200."msg": ""."data": "Object ....",}Copy the code

And our landing returns

Password must be passed! And so onCopy the code

Obviously our return format is not normal for development, because the front desk doesn’t know if this is an error message or a correct return message!

So we simply encapsulate a return class as follows

Add the Base package and create the BaseResult class

import lombok.Data;

@Data
public class BaseResult {
    private int code;//200= success, otherwise failure
    private String msg;// Failure message
    private Object data;// What was returned successfully

    public BaseResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data; }}Copy the code

Controller logged in under modification

    @PostMapping("login")
    public BaseResult login(@RequestParam(value = "user", defaultValue = "") String user,
                            @RequestParam(value = "pass", defaultValue = "") String pass) {
        if (user.equals("")) return new BaseResult(400."Account must be passed!"."");
        if (pass.equals("")) return new BaseResult(400."Password must be passed!"."");
        User user1 = mapper.login(user, pass);
        if (user1 == null) return new BaseResult(500."Incorrect account password!"."");
        user1.setPass("");
        return new BaseResult(200."", user1);
    }
Copy the code

Now let’s look at the return value

{
    "code": 400."msg": "Password must be passed!"."data": ""
}
Copy the code
{
    "code": 200."msg": ""."data": {
        "id": 1."user": "wz"."pass": ""}}Copy the code

With this return format, the front end is easy to parse

Each HTTP status code consists of three digits. The first digit defines the type of the status code

Start 2 :(request successful) indicates that the request status code was successfully processed

200 :(successful) the server has successfully processed the request. Typically, this means that the server has provided the requested web page. 201 :(created) the request was successful and the server created a new resource. 202 :(accepted) the server accepted the request, but has not yet processed it. 203 :(unauthorized information) the server successfully processed the request, but the information returned may be from another resource. 204 :(no content) the server successfully processed the request, but did not return anything 205 :(reset content) the server successfully processed the request, but did not return anything 206 :(partial content) the server successfully processed some GET requests

Start 3 :(request redirected) indicates that further action is required to complete the request. Typically, these status codes are used for redirects

300 :(multiple options) the server can perform multiple operations on a request. The server can select an action based on the user agent or provide a list of actions for the requester to select 301 :(permanent move) the requested web page has been permanently moved to a new location. When the server returns this response (a response to a GET or HEAD request), it automatically redirects the requester to a new location. 302: (Temporary move) The server is currently responding to requests from pages in a different location, but the requester should continue to use the same location for future requests: When the requester should use a separate GET request for a different location to retrieve the response, the server returns this code 304 :(unmodified) the requested page has not been modified since the last request. The server returns this response, not the content of the page 305 :(using a proxy) the requester can only access the requested page using a proxy. If the server returns this response, it also indicates that the requester should use proxy 307 :(temporary redirection) the server is currently responding to the request from a web page in a different location, but the requester continues to use the original location for future requests

Start 4 :(request error) these status codes indicate that the request may be in error, preventing the server from processing it

400 :(error request) the server does not understand the syntax of the request 401 :(unauthorized) the request requires authentication. For web pages requiring login, the server may return this response 403: (Forbidden) The server rejected the request 404: (Not found) The server could not find the requested web page 405: (method disabled) The method specified in the request is disabled 406: (Not accepted) The web page cannot respond to the request using the requested content features 407: (proxy authorization required) this status code is similar to 401 (unauthorized), but specifies that the requester should authorize the use of proxy 408 :(request timeout) the server timed out while waiting for the request 409 :(conflict) the server conflicted while completing the request. The server must include information about the conflict in the response 410: (Deleted) The server will return this response if the requested resource has been permanently deleted 411: (Valid length required) The server will not accept requests without a valid content-length header field 412: (did not meet the prerequisites) server did not meet the requestor in the request set up one of the preconditions of 413: (the request entity is too large) server cannot process the request, because the request entity is too large, beyond the processing capacity of 414 of the server (the request URI is too long) the request URI (usually a url) is too long, the server can’t handle 415: (unsupported media type) request format not supported by the page requested 416 :(request scope not matched) if the page cannot provide the requested scope, the server returns this status code 417 :(not met expectations) the server did not meet the requirements of the “expected” request header field

Start 5 :(server error) these status codes indicate that the server has experienced an internal error while trying to process the request. These errors may be server errors rather than request errors.

500 :(internal server error) the server encountered an error and could not complete the request 501 :(not implemented) the server is not capable of completing the request. For example, the server may return this code 502 when it is unable to recognize the request method :(error gateway) the server acts as a gateway or proxy and receives an invalid response from the upstream server. 503 :(service unavailable) the server is currently unavailable (due to overloading or downtime for maintenance). Usually, this is just a transient state 504 :(gateway timed out) the server acted as a gateway or proxy, but did not receive the request in time from the upstream server 505 :(HTTP version not supported) the server does not support the HTTP protocol version used in the request