Uploading and downloading images with Jersey + hibernate RESTful web service

ⅰ亾dé卋堺 提交于 2019-12-01 20:15:30
  1. That's not valid JSON. The "profilePhoto" you are sending is numbers with letters. That's not a valid type. It can be either a number or a string. If it is to be a string, then wrap it in " quotes.

  2. After you make it a valid string, the deserializer doesn't know how to map the string to a byte[]. You will need to create a custom (de)serializer. With MOXy, you can use an XmlAdapter. An example would something as simple as

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    import java.util.Base64;
    
    public class Base64ByteArrayAdapter extends XmlAdapter<String, byte[]> {
    
        @Override
        public byte[] unmarshal(String base64) throws Exception {
            return Base64.getDecoder().decode(base64);
        }
    
        @Override
        public String marshal(byte[] bytes) throws Exception {
            return Base64.getEncoder().encodeToString(bytes);
        }
    }
    

    The unmarshal will handling converting the incoming base64 string to a byte[], and the marshal will handle the converting of the byte[] to the base64 string.

    Then you just need to annotate the property in the bean with the adapter

    @XmlJavaTypeAdapter(Base64ByteArrayAdapter.class)
    public byte[] getProfilePhoto() {}
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!