Restful Web服务中属性的默认值

时间:2013-01-21 23:59:59

标签: java spring rest

我正在使用Java 1.7的Spring Restful Web服务

我有一个json mapper类,它有fName,lName,address,currentTime等的getter / setter方法

现在,这个类具有以下属性:

@JsonSerialize(include = Inclusion.NON_DEFAULT)

由于此属性,我的其余Web服务不会返回具有默认值的属性。

保留这些价值需要做些什么?

如果我将Inclusion设置为NON_NULL,它可以工作但是它会在heartbeat API中出现问题,我只需要currentTime。

我在同一个杰森映射器类&中为currentTime设置了getter / setter。如果我将包含设置为NON_NULL,则返回所有其他具有默认值的属性。

如何在RESTFul服务中处理此问题?我只想为heartbeat API&返回time属性。对于其他API,我需要所有属性以及默认值。

如果有人能提出建议我会很感激!!

更新:这是我的代码:

Controller.java

@RequestMapping(value = "heartbeat", method = RequestMethod.GET, consumes="application/json")
public ResponseEntity<String> getHeartBeat() throws Exception {
    String curr_time = myService.getCurrentTime();      
    return MyServiceUtil.getResponse(curr_time, HttpStatus.OK);
}

@RequestMapping(value = "info", method = RequestMethod.POST, consumes="application/json")
public ResponseEntity<String> getData(@RequestBody String body) throws Exception {
    ....
    myInfo = myService.getMyInfo(myServiceJson);
    return MyServiceUtil.getResponse(myInfo, responseHeader, HttpStatus.OK);
}

MyService.java

@Override
public String getCurrentTime() throws Exception {
    String currentDateTime = null;
    MyServiceJson json = new MyServiceJson();
    ObjectMapper mapper = new ObjectMapper();

    try {           
        Date currDate = new Date(System.currentTimeMillis());
        currentDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(currDate);           
        json.setCurrentDateTime(currentDateTime);

        ObjectWriter writer = mapper.writerWithView(Views.HeartBeatAPI.class);
        return writer.writeValueAsString(json);

    } catch (Exception e) {
        throw new Exception("Excpetion in getCurrentTime: ", HttpStatus.BAD_REQUEST);           
    }
}

@Override
public String getMyInfo(MyServiceJson myServiceJson) throws Exception {             
    MyServiceJson json = new MyServiceJson();
    json.setFirstName("hhh");
    json.setLastName("abc");

    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(json);
}

Views.java

public class Views {
    public static class HeartBeatAPI {  }
}

MyServiceJson.java

@JsonSerialize(include = Inclusion.NON_NULL)
public class MyServiceJson {
    private int id;
    private String firstName;   
    private String lastName;

    @JsonView(Views.HeartBeatAPI.class) 
    private String currentDateTime;

    // Getter/Setter for the above variables here
    .....

}

当我运行heartbeat API时,我仍然将id值设为0,这是默认值。

1 个答案:

答案 0 :(得分:0)

您可能需要考虑使用@JsonFilter或@JsonView。

使用JsonFilter

@JsonFilter("myBeanFilter")
public class MyBean implements Serializable {
    private String fName;
    private lName;
    private address;
    private Date currentTime;

    // Constructors, Getters/Setters
}

final MyBean bean = new MyBean();
final ObjectMapper mapper = new ObjectMapper();
final FilterProvider filters = new SimpleFilterProvider().addFilter("myBeanFilter",
    SimpleBeanPropertyFilter.filterOutAllExcept("currentTime"));
final String json = mapper.filteredWriter(filters).writeValueAsString(value);

使用JsonView

public class Views {
    static class HeartbeatAPI { }
}

public class MyBean implements Serializable {
    private String fName;
    private lName;
    private address;

    @JsonView(Views.HeartbeatAPI.class)
    private Date currentTime;

    // Constructors, Getters/Setters
}

final MyBean bean = new MyBean();
final ObjectMapper mapper = new ObjectMapper().configure(
    SerializationConfig.Feature.DEFAULT_VIEW_INCLUSION, false);;
final ObjectWriter writer = mapper.writerWithView(Views.HeartbeatAPI.class);
final String json = writer.writeValueAsString(bean);

这两种方法 执行 要求您通过ObjectMapper控制序列化过程。