【问题标题】:Return object as Json in java在java中将对象作为Json返回
【发布时间】:2018-08-09 15:40:06
【问题描述】:

我们希望将 Object 类返回为 JSON 字符串

 public static String genJson(Response r, Auth auth) {
            Auth p = new Auth();
            ObjectMapper mapper = new ObjectMapper();

            String transactionResult = r.getTransactionResult();
            if (transactionResult.equalsIgnoreCase("APPROVED")) {
                p.setPay_resp_resp_code("00");
                p.setPay_resp_desc("Waiting");

                try {
                    jsonStr = mapper.writeValueAsString(p);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
            return jsonStr;
        }

Auth 类中,我有五个变量,但我只希望它显示两个,即resp_coderesp_desc。但是上面的代码总是返回 5 个变量。

编辑

这就是我们的代码假设的工作方式。首先它将 jsonString 转换为名为 Auth 的对象类。然后它将执行 if 语句。

 @RequestMapping("/authstep")
    @ResponseBody
    private String AuthRequest(@RequestBody String jsonString) {

        log.info("========== Receive JSON object request ==========");

        if (jsonString != null) {

            Auth auth = new Auth();

            ObjectMapper mapper = new ObjectMapper();
            try {
                auth = mapper.readValue(jsonString, Auth.class);
            } catch (JsonGenerationException jge) {
                jge.printStackTrace();
            } catch (JsonMappingException jme) {
                jme.printStackTrace();
            } catch (JsonParseException jpe) {
                jpe.printStackTrace();
            } catch (IOException io) {
                io.printStackTrace();
            }

            if (auth.getSignature().equals(signature)) {
               Response response =xxx.getStep(auth);
               return Util.genJson(response, auth);
            }              
        }
        return null;
    }

授权

@Data
public class Auth {

    @JsonIgnore
    private String cipher;

    @JsonIgnore
    private String month;

    @JsonIgnore
    private String signature;

    // need to return these two as json
    private String pay_resp_resp_code;
    private String pay_resp_desc;

}

添加@JsonIgnore后,签名变为空

【问题讨论】:

  • 映射器应该如何知道哪些属性要编码,哪些不应该编码?
  • 有没有办法让映射器知道应该制作哪些属性?
  • 您使用什么库来创建 json?有些人使用@transientannotation 来知道应该忽略哪个变量
  • 我假设您使用的是 Jackson:baeldung.com/jackson-ignore-properties-on-serialization
  • 是的,您可以将 @JsonProperty(access = Access.WRITE_ONLY) private String password; 用于所有其他三个字段

标签: java json objectmapper mapper


【解决方案1】:

如果你使用的是jackson 1.9以下的版本

  Add @IgnoreJson annotation on Getter method

如果您使用的是更新版本的 jackson

@JsonProperty(access = Access.WRITE_ONLY) 
private String password;

【讨论】:

    【解决方案2】:

    如果 Auth 对象中的所有其他属性都为 null,那么这可能会有所帮助:

    mapper.setSerializationInclusion(Include.NON_NULL);
    

    如果其他属性(或其中一些属性)是原语,那么您必须使用注释

    @JsonIgnore
    

    注释。

    或者...如果您无法更改/编辑 Auth 对象,则只需创建一个仅包含所需属性的其他私有对象。

    【讨论】:

    • 似乎您对传入和传出消息使用相同的 Auth 对象,但是某些属性仅在请求中使用,而其他属性仅在响应中使用。使用 @JsonProperty(access = Access.WRITE_ONLY) 和 @JsonProperty(access = Access.WRITE_READ) 注释。或者创建两个单独的对象:AuthIn 和 AuthOut,每个对象都只具有所需的属性。我更喜欢后一种解决方案,它更清晰。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多