使用 Jersey + hibernate RESTful 网络服务上传和下载图像

Uploading and downloading images with Jersey + hibernate RESTful web service

我决定将图像作为字节数组存储在数据库中。 我收到一个错误。

我的实体模型(带有 getter 和 setter):

@Entity
@Table(name="USER", schema="test")
@XmlRootElement
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE)
    @Column(name="user_id")
    private long userId;
    @Lob
    @Column(name="profile_photo")
    private byte[] profilePhoto;
    public User() {
    }
}

然后我就有了创建用户的资源,如下所示:

@Path("/users")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {

    private UserService userService = new UserService();

    @POST
    @Path("/signup")
    public Response signUp(User user) {
        Response response;
        try {
            User newUser = userService.addUser(user);
            response = Response.status(Status.OK)
                    .entity(newUser)
                    .build();
        } catch (IllegalArgumentException e) {
            response = Response.status(Status.BAD_REQUEST)
                    .entity(e.getMessage())
                    .build();
        }
        return response;
    }
}

其中 addUser 方法来自下面的 userService

public User addUser(User newUser) throws IllegalArgumentException {
        Object<User> user = new ObjectImpl<User>();
        User checkUser = user.getObjectByNamedQueryString("User.byEmail", newUser.getUserEmail());
        if (checkUser != null) {
            throw new IllegalArgumentException("User account with this email address already exists.");
        }

        byte[] encodedAvatar = newUser.getProfilePhoto();
        if (encodedAvatar != null) {
            byte[] avatar = Base64.getDecoder().decode(encodedAvatar);
            newUser.setProfilePhoto(avatar);
        }
        return user.addObject(User.class, newUser);
    }

在测试过程中,我拍摄了一张测试图像,转换为Base64字符串并使用this site将其转换为十六进制。但是,我收到以下异常:

    javax.ws.rs.WebApplicationException: HTTP 400 Bad Request
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.readFrom(MOXyJsonProvider.java:708)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:256)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:74)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.server.ContainerRequest.readEntity(ContainerRequest.java:271)
    at org.glassfish.jersey.server.internal.inject.EntityParamValueFactoryProvider$EntityValueFactory.provide(EntityParamValueFactoryProvider.java:96)
    at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)
    at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:94)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:326)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: javax.json.stream.JsonParsingException: Unexpected char 53 at (line no=8, column no=29, offset=220), expecting 'a']
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:1072)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:341)
    at org.eclipse.persistence.jaxb.rs.MOXyJsonProvider.readFrom(MOXyJsonProvider.java:660)
    ... 53 more
Caused by: Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: An error occurred unmarshalling the document
Internal Exception: javax.json.stream.JsonParsingException: Unexpected char 53 at (line no=8, column no=29, offset=220), expecting 'a'
    at org.eclipse.persistence.exceptions.XMLMarshalException.unmarshalException(XMLMarshalException.java:120)
    at org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:147)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:978)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:425)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:375)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:708)
    at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:643)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:339)
    ... 54 more
Caused by: javax.json.stream.JsonParsingException: Unexpected char 53 at (line no=8, column no=29, offset=220), expecting 'a'
    at org.glassfish.json.JsonTokenizer.expectedChar(JsonTokenizer.java:538)
    at org.glassfish.json.JsonTokenizer.readFalse(JsonTokenizer.java:317)
    at org.glassfish.json.JsonTokenizer.nextToken(JsonTokenizer.java:390)
    at org.glassfish.json.JsonParserImpl$ObjectContext.getNextEvent(JsonParserImpl.java:255)
    at org.glassfish.json.JsonParserImpl$StateIterator.next(JsonParserImpl.java:172)
    at org.glassfish.json.JsonParserImpl.next(JsonParserImpl.java:149)
    at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:177)
    at org.glassfish.json.JsonReaderImpl.read(JsonReaderImpl.java:88)
    at org.eclipse.persistence.internal.oxm.record.json.JsonStructureReader.parse(JsonStructureReader.java:139)
    ... 60 more

我在POSTMAN中的实际请求体是:

{
    "userEmail": "example.user@gmail.com",
    "profilePhoto": 6956424f5277304b47676f414141414e5355684555674141414438414141424843415941414143774c70397a414141414247644254554541414c4750432f7868425141414143426a53464a4e414142364a6741416749514141506f41414143413641414164544141414f7067414141366d41414146334363756c4538414141414358424957584d4141426353414141584567466e6e394a534141414257576c555748525954557736593239744c6d466b62324a6c4c6e687463414141414141415048673665473177625756305953423462577875637a703450534a685a4739695a547075637a70745a5852684c7949676544703462584230617a30695745315149454e76636d55674e5334304c6a416950676f6749434138636d526d4f6c4a455269423462577875637a70795a475939496d6830644841364c79393364336375647a4d7562334a6e4c7a45354f546b764d4449764d6a4974636d526d4c584e35626e52686543317563794d6950676f674943416749434138636d526d4f6b526c63324e79615842306157397549484a6b5a6a7068596d39316444306949676f674943416749434167494341674943423462577875637a703061575a6d50534a6f644852774f693876626e4d7559575276596d5575593239744c3352705a6d59764d5334774c79492b4369416749434167494341674944783061575a6d4f6b3979615756756447463061573975506a45384c3352705a6d593654334a705a573530595852706232342b436941674943416749447776636d526d4f6b526c63324e79615842306157397550676f67494341384c334a6b5a6a70535245592b436a777665447034625842745a5852685067704d7769645a4141414f6f456c4551565234416531624333425531526b2b3539793732553143586954456950495151314269676d4542697855626b714154692b3130527033527474714f7475723461456348613532784d50614a723961326a6f3556327247324f744b684b7656425343434b735a577755524b4341704579434d676a4a49534637504f65302b2b2f6d3332773262323765516f6466746a6463382f397a7a6e2f392f2f2f2b63392f7a72336862417a4a3658546170437a4f442f42416b574371534846574b4251766b4677564d43584b4f466454474f4d4f785a53484b6257504b6261546364484442657452686a7a43644837496e3546315a45664c472b3678454a4f505671644f5a335652514f6d6c6a504e4c4746666c4144554c594b5979706f6f7852693675375a787a3341344e4363433452615054462b724d2f775033464f34715354645034744f4e526e74777077506c7a564c78316737583554735a57324579454e4e774b5454614d467058566c35567a48565a7059525942456b584173527364464d696847623246674a6e6f735074304f387768686c51566c68703646564b44796f376f4c4d334a56502f374841316b6c4b4752656d44762f353672574a333732775972685a6d57674a45383244465967346644514d64436369685345396a6b67644a5a58676853774e58374f6d747276554e512b6d4465464f41587945756e6439534b5a58364a6e69584d7355726852415a347733574368546b4d54304c767257574b574e46753274446d78562f374c32516a386257784a51584c436959364a66654433544e427542734d6c78624d363037416a654f36583555696d467677335362705a693471586a79444f5077675a762f7a56687a79726c6d61586d6131386f6d7477764f43384f4444456669554a436a655575744c59664566666956476643494e36583878425168476f656d684a54795a5a2b6a2f2f59644c53325771345165615a6d6749444d4e7a6f4e3861434b514150545052476f474b415066335544306856527350336f3771426a76786d2b6634747a4c45625577744531784e514774696c412b4435384c384a6b4749486e554430582b644a51665570724234415533326e315a2b65586c31546430646a61665146384a79524b386b676230714b63796c516e55444879684a656f6f674778546b6d2b42446c784338453930476467335938616b337457725678734a70596972724b3675316f2b3439636d77595a5651374270302b3355414f6939644a5568704b7142653262566e734354656b6d785a74415232736650716332334b36415336676e67586a4c67597263644b37634431656c6979775162514c7465364c2b4c776a4f69797171702b6b68544237364b542b376751357847346449695758594d5a743365304e6a3658694e3853664658566b736d47554a3259702f6c68384b466c426c64534859426c31384c43723971342f5438753139722b52414f4d5a6c3235733236717a766a76754f44667772784f3258566f79716944686768576462593248347876594f6e3268684551544952596f4847794d49576a4c5579794631534172326c76627a6763332b4659586e65364776646953747a51343961656756567653365541696748674b34483562345663763479587a644c796c38792f616f7151736776577a6744795a6f442b66644233344d334f7a6b352f6645666a65563161576d2f507a67753841325456795067736877374649726d747638383272367672625638737336586c695247682b4858476a5658747255325551566d50464e767a474a594a52506e383267633071545a68474c76565542516b735742635a4d2b524634467661797976706555524a654872365738676a746158356d595a5771452f494973526b6f6f7754516f5137536467716a697769304d714a6957574d772b4563516556374e454550787a5174494e4650524f4f634a6372454374594f75584b7558574e58424f317161785067512b7038433374577870666a4f303368655754413363764b70334564445a624d6a45582f6a4548323955793557586e65356d63694147793759497a7a567a7661546a6f3043524b4b4255455955785447677441473370513966586c7566663131637a6342684f31434d4e3464304c7a5a3532774371332f317352564535525a6d356f524956757036664764705141665a542f676e4a79566e5a4e5671626859444c6d7159646b357748654f413138304f44495a784258634d594d6959333543474b485963715153716d4649486c55426c465367633134426a376a524b3454507662697331533355533462503932722b2b3374376f79314f4c5347636461576a493271465a526a4c39616c6b435637566c3972372b766d565746706f59314d483979334c3144676e584147414a4d443978764444414b6b45335343514b5051584567774b7364734575774a6175554a6c324a636471356e35654664663767767a456b774c6f61524877594f475332462f544e692b743939596f47753877614878757a544f5a6f474a45316776304250775a41514170714f6a44664a447a6d7778483771477335445645784a313630502f486779416a657546384b786e5a7555665833756f726d78476767625a3643347477725a33554a707261586d685a646a4972564a5a4e777753546d473676512f6d7844613448774d6542786761314974664d6a4470495276582b5a41354e77734e7959746f69695479482f4b75414f59532b4b3557426d7338566c4e36586636477273695746587545715a6a7a615948484d4166694753334241776c48494574495a4d454d6d44414177594e4b64514d77646e2b7144657a746875533768464266634a7539313332387a334f2b765454414a6a57723764764c74646e355158755046737a6e477076716c5251733256583456474d3654664443326842794550576a486835775155434a4e5642414c5254776d636e455764556735675156357047593544766a62316d446c306f774d7573416b627669494d36636e3843387779746c41794c4a4f33345661437475336a4d6f6651793359327a66514c47545969496c534c54562f427966466e7a2b3048336c7a4973526a752f46504c384e51324142474b77436d6d727767476b6e4450367363724a7269723356744c356662674a4449546d5a3374687231344e493030386c612f41615969546369764454306f57353241644c2f3073712b5a496e4933745453554d374854434f6d497265322f554a4f726d7a703662304c527354712b425652596b55514236514a5867647935312b44564e324831614b61616e4149383967686c53747255504e37546d57345378645a79634e6564416e32563955304669562b39377558534e476d365344695275363168356158506f64524c48586f4738636151386d716e4d7a2f636646534169692b634e67766d674e41717869723053766f79564c7977634e66737a4435516f6b784d38586264693550396f7363596e32345436667a3362795a4937497a6e5a4c76393866644356596f684b33447457657337467258632f696d582b476939394a45542b6559424457464a78515463456f635a694d747142646e53474e76537a67665331614779314277554d6e327566725170594a795769756c6b4845615a446b4850534538336e6d7745644448564a5a356f575178785743483659784263475067346275366d7862682b516b4f6658566c4333414b764d42326c412f456449787947476c737a744f6c724439306f59454d2f5a75684331534d50667a4d6e682f683676707955686c544d4853386d452b70374d754c794434504978566778566745584b35325142566147357a7755513243496b5246536169565767666751784d4953364e4779666e4f4f73324931503857372b6d72653736384f336a34584843763870763379567476694d365679566834314e43457352496a336b4b3256344174366341547273354848713053362f78584c6a662b4e2b496a504533364e6f38795748793532437178626a5451397444416771514353497974534658497a4c566b4977486578774b704e6951624664634c6d2f6630765150733948416c3772576d5858737850454f424c345a465067494f443064654e78627946373235544948456768726f74363554307031646364483639394e786d755a34656c537a595472334572414b57656e34794d7a75714a4d494f6e4d6e46787277414e496f6850673638626e4542695034686f374f4f4944447977524a7471466d5564526e4d2f473364567a3569355a79646a316b547931722f6549485a316c6b704b706c59626650336f6e736c6341334a34534f4253464343395a634c6b56634a4c46307530354e3342614254486f41794967413463445a4e6c3941504578464f4d43777a5970785235646164303458656e334f7777447835363637684f35557641704f41395967495a4c30663672314138704d76516279757545706a3151346579564853373255366f3337426e6e497034553069446b523038422b462f396553774477454e2b5256794a695251745a6644354474654752784e7a524773747757505441487552425052344342464d716339786f7273574b6e68642b544e614f7a7265544c726a47686a6945483570616479417a364f5638355973686678505149476c7365737a655148714871697357744c592f7448364a73316743374e316e754847684b6335767361666b7a5a777950666178427a6a486f7758306a414b79636753504e65564457354a4c746f47543330326f4b73316e323575496e636544736e324c657666714a6866747731575851637667414b693869456f436b50492b3159777468477566644d784b646a442f595673517941726a546c4f726b34576c322f35484a36626d357462384177764e55586d57534c575353557a4844444a2b2f33462b724a504e7a56733774372f58303869767148554854367775376434386f5548416634364743666978615348414f4f354438313048443347394c7558395538534877597a30775a4f46766335737236396f36585238696c4e724b795277574d72783770635558464641632b776234657853324b746a334744533233756b78385932586b4870635979556e7575615848444d46613563344e3337576c7554737669595879576268396d47753366776b4c6433654e6d4e48314b597675474a66545841376c354e46315441616667433866426a4451653657687266415439524f6451624b635735533846764e756467364e7754326138584351394c57757043423544793041764e6c6833412f6a66552f456e752f2b6c674d63374f6c4d67304f51346c30386d34796e314134477477776a4b577a732f626d6f39356559514c364b5a787841626a6f68644272386875455a37674346514b4b6e4345766d4b4a7658616b514b6e67636339344a58687853554873375669796b3550312f4b6d6d7a50566a337a3434613275707439433774527a497732316a7276625a7a4c744d615444303833304e673042796331683755356b6b336532747a567453714e4a32697a6a43683550574a596a572f78654f73444e61413748424f2b4c586861386632646263336661714e4a6b484465337235785874774b7a646e6c735770744d52746f77495a552b4375642b734e32312f766c6b66434f744833504c6d30395538774f50593937656e6571524d6f5767304e745673686e6c6539706444647447437443712f5a6861766e782b64596d6d624d384a4c71354e35656f55314c43465259616d666e4e38516e446c554c4d314b35444a376f325a3553756469792f6a536e38426f4d705441523849616839686776396f74494e614d75425550796272664d58637568397972754e355072634554745a47594174674f6a7a68305054713851524f34456656375339615546746f6c2f7778365054377442526272654f6d74615863697350592b326b5054384b4d4e3430612b4d71357459747755766b304d72634b4b7a633335375a5350687871504f586d366c65375859313934773036504e364977564d307a38774c4c4d4e6878454e77704d7a6b53396c414a4a6679512f41734732385844774f4f2f5230522b45706e7a567a47745363527a623857577359535a35336d7569335643517a32714d346354347a4861327578494a4f566877562b34634b466d5a3541396e3034572f304a6f6b594f6e63596d4973725354446558786b613845374f736f32306a446a745048786f79654a7262584f4f503476486c56374366526c784c5a6d31364355676578646376624c7a333661452b74686f504661554e2f744a4c712f4e784750305148746e666938426d743749324c534b4939572f676f63474432396f6136516e7361556c7067612b6156314d766d6259534c7077696b70756750344d7a2f41782f2f6a4873453562783070516c2b4d737571382f3142674d724d58462f41477669447730537a3230536c755933317658644b503061372b5075564a7068317853397a6e4161454e347a6f4c2f5769763872444d76304e7466764b665072746a76776342384945732f744d4c5342684759612b5036454c546a495047634c332f3553662f463643544d3465776443314d634b59676b65316f537843546e4d6d68365a734b3079752f5336475630756b67662f38544c447154516d756632705135792b563266424a374e4e6a73424c5a586a3149746e394d37336530764c6e32674a426a5636432b54386c532f4137504459766f7353515877552f5533526c436236623633354438554652386b77426c30704f7936584f4d494a3437385365526275794d356c496672786a4d75674a555172776754374e5a6c3970794b4164432f305a472f69556f6a65463166597a3259426e5a542b7267624d614f4b754273786f597267622b4236524d5854336a384a50414141414141456c46546b5375516d4343
}
  1. 无效 JSON。您发送的 "profilePhoto" 是带字母的数字。那不是一个有效的类型。它可以是数字或字符串。如果它是一个字符串,则将其用 " 引号引起来。

  2. 使它成为有效字符串后,反序列化器不知道如何将字符串映射到 byte[]。您将需要创建一个自定义(反)序列化器。使用 MOXy,您可以使用 XmlAdapter。举个简单的例子

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

    unmarshal 将处理将传入的 base64 字符串转换为 byte[]marshal 将处理将 byte[] 转换为 base64 字符串。

    那么你只需要在bean中用adapter注解属性

    @XmlJavaTypeAdapter(Base64ByteArrayAdapter.class)
    public byte[] getProfilePhoto() {}