我有一个使用Jersey的REST API调用,如下所示:
@GET
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/get/{version}")
public String getData(@PathParam("version") String version, FormDataMultiPart request) {
// My code here
}
事实上,我两者都想要:
1) The version set into the URL (like it is now)
2) The version retrieved from the request object.
I don't want to have two separate inputs.
我有办法做到这一点吗?
假设您使用jQuery和AJAX触发请求,那么您可以这样做:
var vesrion = <retrieve vesrion>
var requestURL = "http://required.url/" + version
$.ajax({
type : 'POST',
url : rquestURL,
cache:false,
processData:false,
contentType:false,
data : new FormData($("#"+formId)[0]) // 'formId' will be the ID of your form
}) ..
这就是您可以同时传递路径参数和表单数据的方法。