【问题标题】:Spring MVC + Jackson - JsonViewSpring MVC + 杰克逊 - JsonView
【发布时间】:2016-10-12 11:06:35
【问题描述】:

我需要从我的模型中公开两组不同的值,所以我实现了 2 个视图

public class Views {

    public static class Small{ }

    public static class Large extends Small { }

}

然后,在我的模型中(所有其他字段都用 JSONIgnore 注释

@JsonView(Views.Small.class)
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id_posto", unique = true, nullable = false)
    public int getIdPosto() {
        return this.idPosto;
    }

    public void setIdPosto(int idPosto) {
        this.idPosto = idPosto;
    }

@JsonView(Views.Large.class)
    @NotNull
    @Column(name = "nome_posto_park")
    public String getNomePosto() {
        return this.nomePosto;
    }
public void setNomePosto(String nomePosto) {
        this.nomePosto = nomePosto;
    }

在我的控制器上,我有 2 种方法:

@RequestMapping(value = "/spots", method = RequestMethod.GET)
    public ResponseEntity<Posto> getSpotStatus(@RequestParam(value = "idPosto") int idPosto,
            @RequestParam(value = "occupied") boolean occupied) {
        Posto posto = postoService.findByIdPosto(idPosto);
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
        mapper.setConfig(mapper.getSerializationConfig()
                .withView(Views.Small.class));
        mapper.convertValue(posto, JsonNode.class);

return new ResponseEntity<Posto>(posto, HttpStatus.OK);

@RequestMapping(value="/spot", method = RequestMethod.GET)
    public ResponseEntity<List<Posto>> getSpotList(@RequestParam (value = "idPiano") int idPiano){
        Piano piano = pianoService.findById(idPiano);

    List<Posto> posti = postoService.showSpotsByFloor(-1, piano);
     ObjectMapper mapper = new ObjectMapper();
        mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
        mapper.setConfig(mapper.getSerializationConfig()
                .withView(Views.Large.class));
    mapper.convertValue(posti, JsonNode.class);

    return new ResponseEntity<List<Posto>>(posti, HttpStatus.OK);
}

Che 结果是一样的...(显然第一个是单个 Posto,第二个是 List,但是模型中的所有字段都被序列化了....

使用视图时我做错了什么?

【问题讨论】:

    标签: java json spring


    【解决方案1】:

    您需要使用同意视图和 @ResponseBody 注释定义生产和消费

    示例:更改您的需要

    @Produces(value = { MediaType.APPLICATION_JSON_VALUE })
    @Consumes(value = { MediaType.APPLICATION_JSON_VALUE })
    public @ResponseBody   public ResponseEntity<List<Posto>> getSpotList(...
    
    //when request, put your client agree request view
    protected HttpEntity<T> headers()
    {
        final HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
        headers.set("application", MediaType.APPLICATION_JSON_VALUE);
        // T define your type here
        return new HttpEntity<T>(headers);
    }
    

    【讨论】:

      【解决方案2】:

      您的问题是 Spring 在 ApplicationContext 启动时实例化了它自己的 Jackson ObjectMapper

      您必须自动装配 Spring 管理的 ObjectMapper 并配置该实例,而不是使用 new 创建您自己的实例。

      private final ObjectMapper mapper;
      
      public MyController(ObjectMapper mapper) {
          this.mapper = mapper;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-03
        • 2013-06-15
        • 2012-06-17
        • 2017-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多