Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Restful web服务中属性的默认值_Java_Spring_Rest - Fatal编程技术网

Java Restful web服务中属性的默认值

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中会出

我使用的是使用Java1.7的SpringRESTfulWeb服务

我有一个json映射器类,它有fName、lName、address、currentTime等的getter/setter方法

现在,此类具有以下属性:

@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
控制序列化过程


首选哪一种?使用JsonView看起来更好,使用JsonView,我是否需要在Views类中包含所有API?在json映射器文件中?此外,它是否需要是静态的?这取决于您对类的注释程度。在大多数情况下,jsonfilter需要的注释比jsonview少。然而,您只是屏蔽了一个属性,所以它将真正取决于您觉得最舒服的属性。您的“视图”类实际上只是用于将bean中的属性标记为组。它只需要我在示例中展示的东西,我在objectmapper中没有看到writeValueUsingView方法。我使用的是Jackson版本1.9.9&objectmapper来自org.codehaus.Jackson.map.objectmapper在我的示例中最后一行不应该出现。您可以安全地删除它(请参见编辑)我仍然不清楚。你举的例子是心跳,对吗?我如何处理需要所有属性的其他API?一个重要的问题是,我的视图是在MyServiceJson.java中定义的,其中包含Json getter/setter…objectmapper存在于其他一些文件中。如何访问other文件中的视图?
@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);