Interface package return spring boot value


Spring Boot Integration Tutorial


Outline

rest will return a variety of data interfaces, if the format of the interface without constraints, is likely to cause confusion.

In a real project, it will typically result in a package type, the package contains the classes http status values, status messages, and the actual data.

Benpian achieve a result wrapper classes.

Class Code Package

Result.java

public class Result implements Serializable {
    
    @SuppressWarnings("unused")
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(Result.class);
    
    private static final long serialVersionUID = -1802122468331526708L;
    private int status = -1;
    private String message = "待处理";
    private Map<String, Object> data = new HashMap<String, Object>();

    public Result(){}

    public Result(int status, String message){
        this.status = status;
        this.message = message;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
    
    public void putData(String key, Object value) {
        data.put(key, value);
    }
    
    public void removeData(String key) {
        data.remove(key);
    }

    @Override
    public String toString() {
        return "Result{" +
                "status=" + status +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}

Explanation

Code should be easy to understand, mainly three members:

  • status - state value, should correspond to the state value (200, 404, etc.) of the HTTP
  • messsage - status messages such as "Page Not Found"
  • data - is a hashmap, can be placed on the key-value

The following will create a real project to demonstrate the use of Result class package.

Create a project

Create a spring boot project

Open Eclipse, create spring starter project spring boot of the project, select the menu: File > New > Project ...pop-up dialog box, select: Spring Boot > Spring Starter Projectwhen configuring dependent, check web, such as do not know how to create a spring boot project, referring to the tutorial: [spring boot hello world (restful Interface) examples].

image

Add code

Project increases the file as shown below

image

Result.java file has been described above.

Control class HelloController.java

HelloController class implements an interface control, for testing the wrapper class:

@RestController
public class HelloController {
    
    @RequestMapping(value="/hello", method = RequestMethod.GET, produces="application/json")
    public ResponseEntity<Result> hello(@RequestParam(value="bad", required=false, defaultValue="false") boolean bad) {
        
        // 结果封装类对象
        Result res = new Result(200, "ok");
        
        if(bad) {
            res.setStatus(400);
            res.setMessage("Bad request");
            
            // ResponseEntity是响应实体泛型,通过它可以设置http响应的状态值,此处返回400
            return new ResponseEntity<Result>(res, HttpStatus.BAD_REQUEST);
        }
        
        // 把结果数据放进封装类
        res.putData("words", "Hello world!");
        
        // ResponseEntity是响应实体泛型,通过它可以设置http响应的状态值,此处返回200
        return ResponseEntity.ok(res);
    }
}

Explanation

We return "Hellow world!" Into the string class returns Result package, a consistent state while the state that the value of the Result of the head in the http response values using ResponseEntitythe set state values.

When requested /hello, returns 200; request /hello?bad=true, returns 400.

run

Eclipse on the left, right click on the project root directory pop-up menu, choose: run as -> spring boot apprun the program. Open Postman access interface, results are as follows:

When requested /hello, returns 200

image

Request /hello?bad=true, return 400

image

to sum up

This paper presents the results wrapper classes for reference in practice.

The complete code

Guess you like

Origin www.cnblogs.com/jinbuqi/p/11014406.html