2016-10-12 12 views
1

ich zwei verschiedene Wertesatz von meinem Modell belichten müssen, so implementiert i 2 AufrufeSpring MVC + Jackson - JSONView

public class Views { 

    public static class Small{ } 

    public static class Large extends Small { } 

} 

Dann ich in meinem Modell setzen (alle anderen Felder sind mit JSONIgnore kommentierten

@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; 
    } 

Auf meinem Controller habe ich 2 Methoden:

@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); 

und

@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 Ergebnis ist das gleiche ... (offensichtlich der erste ist eine einzige Posto und die zweite eine Liste aber alle Felder aus dem Modell serialisiert werden ....

Was mache ich falsch bei der Verwendung von Ansichten?

Antwort

0

Sie definieren müssen produziert und verbrauchen mit mit Blick und Annotation einverstanden @ResponseBody

Beispiel: Ihre

benötigt Ändern
@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); 
} 
Verwandte Themen