Jax-rs json pretty output

前端 未结 6 1626
执笔经年
执笔经年 2020-12-08 22:48

in Java when i use the

@Produces(\"application/json\")

annotation the output is not formated into human readable form. How do i achive that

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 23:35

    If you are using the jersey-media-json-binding dependency, which uses Yasson (the official RI of JSR-367) and JAVAX-JSON, you can introduce pretty printing as follows:

    import javax.json.bind.Jsonb;
    import javax.json.bind.JsonbBuilder;
    import javax.json.bind.JsonbConfig;
    import javax.ws.rs.ext.ContextResolver;
    import javax.ws.rs.ext.Provider;
    
    @Provider
    public class RandomConfig implements ContextResolver {
        private final Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withFormatting(true));
    
        public RandomConfig() { }
    
        @Override
        public Jsonb getContext(Class objectType) {
            return jsonb;
        }
    }
    

提交回复
热议问题