Enum对象 作为参数如何传递? postman请求

在请求postman的时候,Enum对象作为参数如何传递?

 

要求:

传递orders对象,用于es排序:

private String fieldName;

private OrderBy orderBy;
public enum OrderBy {
    ASC("asc"), DESC("desc");
}

Postman请求的时候,参数该如何传?

 

ReportOrder:

public class ReportOrder {

    private String fieldName;

    private OrderBy orderBy;

    public ReportOrder(String fieldName, OrderBy orderBy) {
        this.fieldName = fieldName;
        this.orderBy = orderBy;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public OrderBy getOrderBy() {
        return orderBy;
    }

    public void setOrderBy(OrderBy orderBy) {
        this.orderBy = orderBy;
    }

    public enum OrderBy {
        ASC("asc"), DESC("desc");

        String order;

        OrderBy(String order) {
            this.order = order;
        }

        public String getOrder() {
            return order;
        }
    }


    @Override
    public String toString() {
        return "ReportOrder{" +
                "fieldName='" + fieldName + '\'' +
                ", orderBy=" + orderBy +
                '}';
    }
}

 

测验

思考:

orderBy是一个类对象,那传的时候,当作一个对象?

 

代码;

public static void main(String[] arg) {
    String orders = "{\"fieldName\":\"created_date\",\"orderBy\":{\"order\":\"asc\"}}";
    ReportOrder reportOrder;
    reportOrder = JSON.parseObject(orders, ReportOrder.class);
    System.out.println(reportOrder.toString());
}

报错,解析不了。在Enum对象引用的时候,是ReportOrder.OrderBy.ASC ,不是get的方式,而是直接获取,试试直接的方式

public static void main(String[] arg) {
    String orders = "{\"fieldName\":\"created_date\",\"orderBy\":\"asc\"}";
    ReportOrder reportOrder = JSON.parseObject(orders, ReportOrder.class);
    System.out.println(reportOrder.toString());
    orders = "{\"fieldName\":\"created_date\",\"orderBy\":\"asii\"}";
    reportOrder = JSON.parseObject(orders, ReportOrder.class);
    System.out.println(reportOrder.toString());
}

结果:

ReportOrder{fieldName='created_date', orderBy=ASC}
ReportOrder{fieldName='created_date', orderBy=null}

Postman请求的时候:

{
	"fieldName": "create_date",
	"orderBy": "ASC"
}

 

总结:

   在使用enum作为参数传递的时候,将其看做字符串就行,具体的值跟代码设置的值相对应。

 

你可能感兴趣的:(java,Enum对象,postman请求,参数传递)