2017-06-28 2 views
0

Ich versuche, eine Springboot Usermanagement-Anwendung zu erstellen.Senden von Entity-Objekt in Antwort, die Blob enthält

Ich habe ein Entitätsobjekt, das zwei Blob-Elemente enthält. Hier ist mein Entitätsobjekt.

@Entity 
    @Table(name="user_meta_profile") 
    public class UserMetaProfile implements Serializable { 
     private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "user_id") 
    private int user_id; 

    @Column(name = "resume_file") 
    @Lob 
    private Blob resume_file; 

    @Column(name = "photo") 
    @Lob 
    private Blob photo; 

    @Column(name = "username") 
    private String username; 

    public int getUser_id() { 
     return user_id; 
    } 

    public void setUser_id(int user_id) { 
     this.user_id = user_id; 
    } 

    public Blob getResume_file() { 
     return resume_file; 
    } 

    public void setResume_file(Blob resume_file) { 
     this.resume_file = resume_file; 
    } 

    public Blob getPhoto() { 
     return photo; 
    } 

    public void setPhoto(Blob photo) { 
     this.photo = photo; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 
} 

Wie Sie sehen, gibt es zwei ‚resume_file‘ Blob Artikel und ‚Foto‘.

Ich möchte eine JSON-Antwort an den API-Aufruf zurücksenden.

Mein Controller-Code ist wie folgt.

@Controller 
    @RequestMapping("/v1") 
    public class UsersController { 

    @Autowired 
     private IUserMetaProfileService userMetaProfileService; 


    @GetMapping("MetaProfile/{id}") 
     public ResponseEntity<UserMetaProfile> getUserMetaProfileById(@PathVariable("id") Integer id) { 
      UserMetaProfile userMetaProfile = userMetaProfileService.getUsersById(id); 
      return new ResponseEntity<UserMetaProfile>(userMetaProfile, HttpStatus.OK); 
     } 

    } 

Aber wenn ich die API aufrufen, erhalte ich die Ausnahme:

"exception": "org.springframework.http.converter.HttpMessageNotWritableException", 

"message": "Could not write JSON document: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: 
... 

    ...nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) 
+0

Sie versuchen, einer JSON-Zeichenfolge eine Binärdatei hinzuzufügen. Das ist nicht möglich. Wenn Sie wirklich binäre Dateien zu einem JSON-String hinzufügen möchten, müssen Sie die Binärdateien mit base64 codieren. – Eich

Antwort

1

Da JSON nicht binäre Daten enthalten, die Sie brauchen sonst die Felder als etwas serialisieren. Sie haben ein paar Optionen:

  1. Wenn Sie beabsichtigen, die Binärdatei als ein Bild zu zeigen (da Ihr Foto ist), können Sie es als Daten-URI serialisieren.
  2. Senden Sie stattdessen Links zu Fotos und erstellen Sie eine Controller-Methode, die die Binärdaten mit dem entsprechenden Inhaltstyp (über den hier angegebenen Bereich hinaus) ausgibt.

Also für 1 Option können Sie etwas tun:

@Entity 
@Table(name="user_meta_profile") 
public class UserMetaProfile implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "user_id") 
    private int user_id; 

    @Column(name = "resume_file") 
    @Lob 
    private Blob resume_file; 

    @Column(name = "photo") 
    @Lob 
    private Blob photo; 

    @Column(name = "username") 
    private String username; 

    public int getUser_id() { 
     return user_id; 
    } 

    public void setUser_id(int user_id) { 
     this.user_id = user_id; 
    } 

    @JsonIgnore // disable serializing this field by default 
    public Blob getResume_file() { 
     return resume_file; 
    } 

    // serialize as data uri insted 
    @JsonProperty("resumeData") 
    public String getResume() { 
     // just assuming it is a word document. you would need to cater for different media types 
     return "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64," + new String(Base64.getEncoder().encode(resume_file.getBytes())); 
    } 

    public void setResume_file(Blob resume_file) { 
     this.resume_file = resume_file; 
    } 

    @JsonIgnore // disable this one too 
    public Blob getPhoto() { 
     return photo; 
    } 

    // serialize as data uri instead 
    @JsonProperty("photoData") 
    public String getPhotoBase64() { 
     // just assuming it is a jpeg. you would need to cater for different media types 
     return "data:image/jpeg;base64," + new String(Base64.getEncoder().encode(photo.getBytes())); 
    } 

    public void setPhoto(Blob photo) { 
     this.photo = photo; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 
} 

Für das Foto-Bit den Wert des photoData JSON-Attribut kann direkt als src Attribut eines img-Tag und dem Foto eingestellt werden wird im HTML gerendert. Mit der Wiederaufnahme-Datei können Sie es als href zu einem <a>-Tag mit einem download Attribute befestigen, damit sie heruntergeladen werden können:

<a href={photoData value here} download>Download Resume File</a> 

Nur als FYI, wenn die Dateien groß sind die JSON sehr groß sein werden und es könnte auch verlangsamen den Browser.

+0

Danke, Strelok.Es hat funktioniert. – codeLearner

Verwandte Themen