我已经编写了一个java REST Web服务,它正在接受我在header param中发送的图像文件和json数据,所以我想知道如何编写一个客户端来成功调用该Web服务。我的问题是如何在header param中传递图像文件和json数据来调用该webservice。我如何在request.please中找到我必须公开的rest webservice的代码。
@Path("/KVBSvasth")
public class KvbSvasthWebServices {
private static final String FILE_UPLOAD_PATH = "D:\\home";
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/getKvbMemberDetails")
public String saveClaimSubmissionMultiplePdf(@Context HttpServletRequest request,@HeaderParam("jsondataaa")String jsondata) throws Exception
{
WebServiceManager webServiceManager=null;
try{
KvbSvasthVO kvbSvasthVO=null;
String msg="";
String enrollmentID="";
System.out.println("json Data get "+jsondata);
//try{
ObjectMapper mapper = new ObjectMapper();
kvbSvasthVO= mapper.readValue(jsondata, KvbSvasthVO.class);
//}catch(JsonParseException | JsonMappingException jsonexception){
// msg="Data parsing exception";
//return this.getJsonOutputString(msg, enrollmentID);
//}
if (ServletFileUpload.isMultipartContent(request)) {
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload fileUpload = new ServletFileUpload(factory);
final List items = fileUpload.parseRequest(request);
if(items.size()>0){
if (items != null) {
System.out.println("its should 2");
final Iterator iter = items.iterator();
while (iter.hasNext()) {
System.out.println("its should 3");
final FileItem item = (FileItem) iter.next();
final String itemName = item.getName();
item.getFieldName();
item.getString();
String strFileExt = itemName.substring(itemName.lastIndexOf(".") + 1,itemName.length());
File folder = new File(FILE_UPLOAD_PATH);
webServiceManager=this.getWebServiceManagerObject();
//kvbSvasthVO.setFileDataOInputStream(item.getInputStream());
// kvbSvasthVO.setImageFileSize((int)item.getSize());
//kvbSvasthVO.setJsonData(jsondata);
webServiceManager.saveKvbSvasthCustDetails(kvbSvasthVO,item,jsondata);
if (!folder.exists()) {
System.out.println("its should 4");
folder.mkdir();
} // End Of if(!folder.exists())
if (!(strFileExt.equalsIgnoreCase("pdf"))) {
final File savedFile = new File(FILE_UPLOAD_PATH+ File.separator + itemName);
System.out.println("final file "+FILE_UPLOAD_PATH+ File.separator + itemName);
item.write(savedFile);
}
break;
}//end of while (iter.hasNext())
} // end of if (items != null)
}//end of if(items.size()>0)
}
}catch(Exception e){
System.out.println("exception occurs: ");
e.printStackTrace();
}
return jsondata;
}
public String getJsonOutputString(String message,String enrollementID){
JSONObject jsobj = new JSONObject();
LinkedHashMap<String, String> object = new LinkedHashMap();
//object.put("status", status);
object.put("msg",message);
object.put("enrollemntId",enrollementID);
jsobj.putAll(object);
return jsobj.toJSONString();
}
private WebServiceManager getWebServiceManagerObject() throws TTKException
{
WebServiceManager webServiceManager = null;
try
{
if(webServiceManager == null)
{
InitialContext ctx = new InitialContext();
webServiceManager = (WebServiceManager) ctx.lookup("java:global/TTKServices/business.ejb3/WebServiceManagerBean!com.ttk.business.webservice.WebServiceManager");
}//end if(webServiceManager == null)
}//end of try
catch(Exception exp)
{
throw new TTKException(exp, "webservice");
}//end of catch
return webServiceManager;
}//end getWebServiceManagerObject()
}
请帮助,谢谢。
答案 0 :(得分:1)
使用标头传递json数据不是最好的做法 - 它们应该转到正文。但是,让我们假设你有充分的理由。 为了传递图像,您需要先将其转换为字节数组。 以下是如何在java中设置标头值的示例:
HttpClient httpClient = HttpClients.createDefault()
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("jsondataaa", headerData);
HttpEntity requestEntity = new ByteArrayEntity(requestContent);
Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path);
HttpEntity requestEntity = new ByteArrayEntity(data);
httpPost.setEntity(requestEntity);
logXmlRequestResponse(requestContent, operationType + " Request");
HttpResponse execute = httpClient.execute(httpPost);
httpPost.releaseConnection();
答案 1 :(得分:0)
好的我已经恢复了。下面的客户端代码工作正常。
document.location.href