NutzCN Logo
问答 想把pojo中枚举字段在返回json时直接以里面的descrp字段输出字符串
发布于 938天前 作者 wx_latcg0cou3lvl3g2g3a6 1259 次浏览 复制 上一个帖子 下一个帖子
标签:

想把pojo中枚举字段在server @Ok返回json时直接以枚举里面的descrp字段输出字符串,
用了forceString=true和@ToJson, 但是未能如愿,应该怎么用?

    @ToJson
    public enum PatrolDirection {
        EAST(1, "东"),
        SOUTH(2, "南"),
        WEST(3, "西"),
        NORTH(4, "北"),
        EAST_SOUTH(12, "东南"),
        SOUTH_WEST(23, "西南"),
        WEST_NORTH(34, "西北"),
        NORTH_EAST(41, "东北");

        public final int patrol_direction;
        public final String descrp;

        PatrolDirection(int patrol_direction, String descrp) {
            this.patrol_direction = patrol_direction;
            this.descrp = descrp;
        }
        
        public String toJson() {
        	log.info("toJson() called !!!!!!!!!!!!!!"); 
        	return getDescrp();
        }
        
        public String toJson(JsonFormat jf) {
        	log.info("toJson() called !!!!!!!!jf!!!!!!"); 
        	return getDescrp();
        }
        
        

        public static PatrolDirection getFromInt(Integer v) {
            if(v==null){
                return PatrolDirection.EAST;
            }

            for(PatrolDirection pt : values()){
                if(pt.patrol_direction==v){
                    return pt;
                }
            }
            return PatrolDirection.EAST;
        }

        public int getValue() {
            return patrol_direction;
        }


        public String getDescrp(){
            return descrp;
        }

        public static PatrolDirection getFromText(String text){
            if(text==null){
                return null;
            }

            for(PatrolDirection pt : values()){
                if(pt.descrp.equals(text)){
                    return pt;
                }
            }
            return null;

        }
@JsonField(forceString=true)
	@Column
	@ColDefine(type=ColType.INT, adaptor=PatrolDirectionAdaptor.class)
    public PatrolDirection patrol_direction; //巡检方向
7 回复

是报错还是咋了?

它直接输出 SOUTH, 期望按自定义输出 南
而且 log.info("toJson() called !!!!!!!!!!!!!!"); 并没有被调用

server端返回: {"direction":"SOUTH"}

@At("/test")
@Ok("json")
@Filters(@By(type=CrossOriginFilter.class))
public class TestModule {
    @At
    public Object test_direction(HttpServletRequest req) {
    	NutMap re = new NutMap();
    	re.put("direction", PatrolDirection.SOUTH);
    	return re;
    }
}

去掉toJson系列方法试试

换个写法 , 服务端返回 {"data":{"id":0,"patrol_direction":"SOUTH"}}
一样的。

我的nutzboot是 2.4.0.v20200427


@Table("test_tble_xxx") public class TestBean { @Id public long id; @JsonField(forceString=true) @Column @ColDefine(type=ColType.INT, adaptor=PatrolDirectionAdaptor1.class) public PatrolDirection1 patrol_direction; //巡检方向 public static class PatrolDirectionAdaptor1 implements ValueAdaptor { @Override public Object get(ResultSet rs, String colName) throws SQLException { // TODO Auto-generated method stub int databaseValue = rs.getInt(colName); return PatrolDirection.getFromInt(databaseValue); } @Override public void set(PreparedStatement stat, Object obj, int index) throws SQLException { // TODO Auto-generated method stub PatrolDirection p = (PatrolDirection) obj; stat.setInt(index, p.getValue()); } } }

@At("/test") @Ok("json") @Filters(@By(type=CrossOriginFilter.class)) public class TestModule { @At public Object test_direction(HttpServletRequest req) { NutMap re = new NutMap(); TestBean data = new TestBean(); data.patrol_direction = PatrolDirection1.SOUTH; re.put("data", data); return re; } }

去掉 toJson 也是一样。 它里面的日志都没打印执行

不支持enum??

我用普通class可以正常输出,但enum有问题:
{"data":{"id":0,"patrol_direction":"SOUTH","patrol_direction2":南}}


@Table("test_tble_xxx") public class TestBean { @Id public long id; @Column @ColDefine(type=ColType.INT, adaptor=PatrolDirectionAdaptor1.class) public PatrolDirection1 patrol_direction; //巡检方向 @Column @ColDefine(type=ColType.MYSQL_JSON) public PatrolDirection2 patrol_direction2; public static class PatrolDirectionAdaptor1 implements ValueAdaptor { @Override public Object get(ResultSet rs, String colName) throws SQLException { // TODO Auto-generated method stub int databaseValue = rs.getInt(colName); return PatrolDirection.getFromInt(databaseValue); } @Override public void set(PreparedStatement stat, Object obj, int index) throws SQLException { // TODO Auto-generated method stub PatrolDirection p = (PatrolDirection) obj; stat.setInt(index, p.getValue()); } } }
添加回复
请先登陆
回到顶部