简体   繁体   English

java webservice可以同时接受json和文件上传?

[英]java webservice to accept json and file upload at the same time?

I have the following web service that works just fine. 我有以下工作正常的Web服务。 I need to add the capability to upload a file along with the instance of TrackBean that is serialized into JSON. 我需要添加功能,以上传文件以及序列化为JSON的TrackBean实例。

I've found a lot of examples of just file uploads but none that would accept json and a file within the same post. 我发现了很多仅上传文件的示例,但没有一个示例可以在同一帖子中接受json和文件。

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. 更好的做法是先上传文件,从服务器获取某种令牌作为响应,然后在第二个帖子中发送json,并引用帖子#1的令牌,以便服务器知道要与哪个文件关联传入的JAXB_TrackBean实例。

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: 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 

 }

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. 当我必须同时在Web服务请求中包含文件时,通常我会接受文件内容为字节数组或以64为基数的编码内容。 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. 值得一提的是,我尝试只通过SOAP WS来了解这种方法,但是我看不出为什么它不适用于REST + JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM