Java Restful web服务中属性的默认值
我使用的是使用Java1.7的SpringRESTfulWeb服务 我有一个json映射器类,它有fName、lName、address、currentTime等的getter/setter方法 现在,此类具有以下属性:Java Restful web服务中属性的默认值,java,spring,rest,Java,Spring,Rest,我使用的是使用Java1.7的SpringRESTfulWeb服务 我有一个json映射器类,它有fName、lName、address、currentTime等的getter/setter方法 现在,此类具有以下属性: @JsonSerialize(include = Inclusion.NON_DEFAULT) 由于该属性,我的RESTWeb服务不返回具有默认值的属性 需要做些什么来保留这些值 如果我将Inclusion设置为NON_NULL,它会工作,但在heartbeat API中会出
@JsonSerialize(include = Inclusion.NON_DEFAULT)
由于该属性,我的RESTWeb服务不返回具有默认值的属性
需要做些什么来保留这些值
如果我将Inclusion设置为NON_NULL,它会工作,但在heartbeat API中会出现问题,我只需要currentTime
我在同一个jason映射器类中使用了currentTime的getter/setter&如果我将inclusion设置为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);
}
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
.....
}
当我运行心跳API时,我仍然将ID值设为0,这是默认值。
您可能需要考虑使用@ jsNoFor或@ jSnVIEW。< /P> 使用JsonFilter 使用JsonView 这两种方法都要求您通过ObjectMapper
控制序列化过程
@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
.....
}
@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);
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);