0

I have the following web service that works just fine. I need to add the capability to upload a file along with the instance of TrackBean that is serialized into JSON.

I've found a lot of examples of just file uploads but none that would accept json and a file within the same post.

Is it bad technique to do this all at once? Would it be a better practice to upload the file first, get some sort of token from the server as a response and then send the json in a second post, referencing the token from post #1 so the server would know which file to associate with the incoming JAXB_TrackBean instance.

Thanks in advance for the help!

webservice chunk:

@POST
@Path( "/post" )
@Consumes( MediaType.APPLICATION_JSON )
public Response createTrackInJSON( JAXB_TrackBean track )
{

    String result = "TrackBean saved : " + track.getText() ;
    return Response.status( 201 ).entity( result ).build();

}

JAXB_TrackBean:

@XmlRootElement( name = "track" )
@XmlType( propOrder = { "id", "text" } )
public class JAXB_FtTextBean
{
     private long id = 0;
     private String text;

        // getter/setters omitted for brevity 

 }
cotfessi
  • 167
  • 2
  • 12
  • 2
    A rather good response can be found [here](http://stackoverflow.com/questions/12022925/uploading-json-and-binary-file-in-one-request). – Olimpiu POP Feb 08 '13 at 21:13
  • that post still only goes into a webservice that accepts a file upload... i'm looking for one that does both a file and json string – cotfessi Feb 09 '13 at 00:16

1 Answers1

0

What I typically did when I had to include also I file in a webservice request is that I accepted the file content as bytes array or base 64 encoded content. In this fashion the file was just another parameter of the call. Not the most efficient way of doing it, but provided results. Worth being mentioned that this approach I tried up to know only with SOAP WSs, but I don't see a reason why it shouldn't work with REST+JSON.

Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49