我正在尝试开发一个屏幕,将json和image作为同一请求的一部分发送。
我可以在两个不同的调用中发送json和image,但我需要同时发送它们。
下面是我的屏幕,其中包含我的java脚本代码。我使用apache CXF作为我的休息组件。
var table = $('#familyDetails').tableToJSON();
var kovilRequestDTO={
"familyDetailsList" : table,
};
var data=JSON.stringify(kovilRequestDTO);
var formData = new FormData();
formData.append('kovilRequestDTO', data);
formData.append('familHeadImage', $('input[type=file]')[0].files[0]);
$.ajax({
type: "POST",
url: "http://localhost:8091/KovilRestServices/kovilRestService/uploadFile",
data: formData,
contentType: false,
processData: false,
dataType: "json",
success: function(response) {
$( "#message-box h2" ).html("Your registration is successful."+response.familyHeadId);
$(".message-box").fadeIn("fast");
}
});

<body>
<input id="add_new" type="button" value="Add Record" />
<input type="button" class="glyphicon glyphicon-plus" id="my-button" value="Add image">
<!-- <input type="file" name="my_file" id="my-file"> -->
<form style="display:none" name="fileUploadForm" id="fileUploadForm"
action="http://localhost:8091/KovilRestServices/kovilRestService/uploadFile"
method="post" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="my_file" id="my_file" size="50" />
</p>
<input type="hidden" name="familyHeadId"/>
<input type="submit" value="Upload It" />
</form>
</body>
</html>
<div class="addmembertable">
<table id="familyDetails" class="table-list" border="0" width="70%"
cellspacing="0" cellpadding="0">
<tbody>
<tr>
<th>Name</th>
<th>Relation</th>
<th>DOB</th>
<th>Gender</th>
<th>Qualification</th>
<th>PID</th>
<th>Profession</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
<th>Delete</th>
</tr>
</tbody>
</table>
</div>
<div id="message-box" class="message-box" style="display: none;">
<h2></h2>
<table>
<tr>
<td align="right"></td>
<td><input type="button" value="OK" id="OK"></td>
</tr>
</table>
</div>
<div class="entry-form">
<form name="userinfo" id="userinfo">
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr>
<td colspan="2" align="right"><a href="#" id="close">Close</a></td>
</tr>
<tr>
<td>Name</td>
<td><input type="text" class="name" name="name"></td>
</tr>
<tr>
<td>Relation</td>
<td><select id="relation" class="relation">
<option selected>--select--</option>
<option value="Family Head">Family Head</option>
<option value="Son">Son</option>
<option value="Daughter">Daughter</option>
<option value="Wife">Wife</option>
<option value="Mother">Mother</option>
<option value="Father">Father</option>
</select></td>
</tr>
<tr>
<td>DOB</td>
<td><input type="text" class="dob" name="dob"></td>
</tr>
<tr>
<td>Relation</td>
<td><select id="gender" class="gender" name="gender">
<option selected>--select--</option>
<option>Male</option>
<option>Female</option>
</select></td>
</tr>
<tr>
<td>Qualification</td>
<td><input type="text" class="qualification"
name="qualification"></td>
</tr>
<tr>
<td>Parental ID</td>
<td><input type="text" class="parentalID" name="parentalID"></td>
</tr>
<tr>
<td>Profession</td>
<td><input type="text" class="profession" name="profession"></td>
</tr>
<tr>
<td>Phone Number</td>
<td><input type="text" class="phone" name="phone"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" class="email" name="email"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" class="address" name="address"></td>
</tr>
<tr>
<td align="right"></td>
<td><input type="button" value="Save" id="save"><input
type="button" value="Cancel" id="cancel"></td>
</tr>
</table>
</form>
</div>
<div class="addmemberfooter">
<input id="submit" type="button" value="Submit" />
</div>
</body>
&#13;
我的休息实施如下。我调用它时遇到404错误。正在工作的其他休息端点以相同的方式配置。我已经使用apache CXF进行休息实现
@POST
@Path("/uploadFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("application/json")
public Response testDownload(
@FormParam(value="kovilRequestDTO") KovilRequestDTO kovilRequestDTO,
@FormParam(value="familHeadImage") Attachment attachment);
错误堆栈跟踪:
WARNING: No operation matching request path "/KovilRestServices/kovilRestService/uploadFile" is found, Relative Path: /uploadFile, HTTP Method: POST, ContentType: multipart/form-data;boundary=----WebKitFormBoundaryAmzgeu8Y86sBX7m2, Accept: */*,. Please enable FINE/TRACE log level for more details.