我试图将java对象发布到.net核心web api中,但我需要用java发布对象数组
因此,我尝试从Java编写一个表单post。到我的.net核心webapi。直到我添加了发送内部对象数组的要求,表单post才运行得很好。我想相信,当我发表文章时,我的.net并没有理解它接收到的有效载荷。因此,下面的java代码将值发送到.NETCore。在假定收集(绑定)来自java代码的请求的模型之后,也将粘贴该请求 //此方法将把值发布到.net我试图将java对象发布到.net核心web api中,但我需要用java发布对象数组,java,javafx,Java,Javafx,因此,我尝试从Java编写一个表单post。到我的.net核心webapi。直到我添加了发送内部对象数组的要求,表单post才运行得很好。我想相信,当我发表文章时,我的.net并没有理解它接收到的有效载荷。因此,下面的java代码将值发送到.NETCore。在假定收集(绑定)来自java代码的请求的模型之后,也将粘贴该请求 //此方法将把值发布到.net public <T> T Handlepost(File inFile,Productmodel model,final Cl
public <T> T Handlepost(File inFile,Productmodel model,final Class<T> objectClass) {
FileInputStream fis = null;
try {
StringBody name = new StringBody(model.getName());
StringBody barcode = new StringBody(model.getBarcode());
StringBody quantity = new StringBody(model.getStockQuantity()+"");
StringBody unitcost = new StringBody(model.getUnitCost()+"");
StringBody discountamt = new StringBody(model.getDiscountamt()+"");
StringBody describe = new StringBody(model.getDescription());
StringBody companyid = new StringBody(model.getCompanyID()+"");
StringBody unitid = new StringBody(model.getUnit().getID());
StringBody inventorycatid = new StringBody(model.getProductposcategory().getID());
String hascolors = "";
if(model.getHascolortypes()){
hascolors = "yes";
}
else
hascolors = "no";
StringBody hascolortypes = new StringBody(hascolors);
if(inFile != null){
fis = new FileInputStream(inFile);
}
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
String Posturl = urlobject.Createproducturl();
// server back-end URL
HttpPost httppost = new HttpPost(Posturl); //"http://localhost:56175/api/product/create"
MultipartEntity entity = new MultipartEntity();
// set the file input stream and file name as arguments
if(inFile != null)
entity.addPart("Image", new InputStreamBody(fis, inFile.getName()));
entity.addPart("Name", name);
entity.addPart("Barcode",barcode);
entity.addPart("StockQuantity", quantity);
entity.addPart("UnitCost", unitcost);
entity.addPart("Discountamt", discountamt);
entity.addPart("Description", describe);
entity.addPart("CompanyID", companyid);
entity.addPart("Unit.ID",unitid);
entity.addPart("Hascolortypes", hascolortypes);
entity.addPart("InventorycategoryID",inventorycatid);
// Gson gson = new Gson();
/* String json = new JSONArray(model.getCostpersizes()).toString(); //gson.toJson(model.getCostpersizes());
StringBody productprices = new StringBody(json);
entity.addPart("Costpersizes", productprices);*/
public T Handlepost(文件填充、产品模型、最终类objectClass){
FileInputStream fis=null;
试一试{
StringBody名称=新的StringBody(model.getName());
StringBody条码=新的StringBody(model.getBarcode());
StringBody数量=新的StringBody(model.getStockQuantity()+);
StringBody单位成本=新的StringBody(model.getUnitCost()+);
StringBody Dispentamt=新的StringBody(model.getDispentamt()+);
StringBody description=新的StringBody(model.getDescription());
StringBody companyid=新的StringBody(model.getCompanyID()+);
StringBody unitid=新的StringBody(model.getUnit().getID());
StringBody inventorycatid=新的StringBody(model.getProductposcategory().getID());
字符串haskolors=“”;
if(model.getHascolortypes()){
haskolors=“是”;
}
其他的
haskolors=“否”;
StringBody hascolortypes=新的StringBody(hascolors);
if(infle!=null){
fis=新文件输入流(infle);
}
DefaultHttpClient httpclient=新的DefaultHttpClient(新的BasicHttpParams());
字符串Posturl=urlobject.Createproducturl();
//服务器后端URL
HttpPost HttpPost=新的HttpPost(姿势);/“http://localhost:56175/api/product/create"
多方实体=新多方实体();
//将文件输入流和文件名设置为参数
if(infle!=null)
entity.addPart(“Image”,新的InputStreamBody(fis,infle.getName());
实体。添加部分(“名称”,名称);
实体。添加部件(“条形码”,条形码);
实体.添加部分(“库存数量”,数量);
实体。增加部分(“单位成本”,单位成本);
实体。添加部分(“折扣”,折扣);
实体。添加部分(“说明”,描述);
实体。添加部分(“公司ID”,公司ID);
实体添加部分(“单位ID”,unitid);
entity.addPart(“Hascolortypes”,Hascolortypes);
entity.addPart(“InventorycategoryID”,inventorycatid);
//Gson Gson=新的Gson();
/*字符串json=newJSONArray(model.getCostpersizes()).toString();//gson.toJson(model.getCostpersizes());
StringBody productprices=新的StringBody(json);
实体。添加部分(“成本”,产品价格)*/
//这是内部对象的数组。我使用它收集用户的多个选择。因此,我必须将有效负载内的对象数组发送到.net控制器
//Adding array to the Product post object
if(model.getCostpersizes() != null ){
for(int k=0; k<model.getCostpersizes().size(); k++){
Productpricemodel pricemodel = model.getCostpersizes().get(k);
entity.addPart("Costpersizes[].SN", new StringBody(pricemodel.getSN()));
entity.addPart("Costpersizes[].ProductID", new StringBody(pricemodel.getProductID()));
entity.addPart("Costpersizes[].AvailableQuantity", new StringBody(pricemodel.getAvailableQuantity()+""));
entity.addPart("Costpersizes[].Barcode", new StringBody(pricemodel.getBarcode()));
entity.addPart("Costpersizes[].Cost", new StringBody(pricemodel.getCost()+""));
entity.addPart("Costpersizes[].Size.ID", new StringBody(pricemodel.getSize().getID()));
entity.addPart("Costpersizes[].Response", new StringBody(pricemodel.getResponse()));
}
}
System.out.println("This is the string reprensentaion of the object that was sent to the server");
System.out.println(entity.toString());
httppost.setEntity(entity);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Accept-Encoding", "gzip");
if(Main.LicenseToken != null){
httppost.setHeader("Authorization",Main.LicenseToken); //I just added this here
}
// execute the request
HttpResponse httpResponse = httpclient.execute(httppost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
HttpEntity responseEntity = httpResponse.getEntity();
/*
String responseString = EntityUtils.toString(responseEntity, "UTF-8");
System.out.println("[" + statusCode + "] " + responseString);*/
if (responseEntity != null) {
InputStream inputStream = responseEntity.getContent();
Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
String resultString = convertStreamToString(inputStream);
System.out.println("output "+resultString);
inputStream.close();
return new GsonBuilder().create().fromJson(resultString, objectClass);
}
} catch (ClientProtocolException e) {
System.err.println("Unable to make connection");
e.printStackTrace();
} catch (IOException e) {
System.err.println("Unable to read file");
e.printStackTrace();
}
finally {
try {
if (fis != null) fis.close();
} catch (IOException e) {}
}
return null;
}
private String convertStreamToString(InputStream inputStream) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
} catch (IOException e) {
System.out.println("first "+e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
inputStream.close();
} catch (IOException e) {
System.out.println("second "+e.toString());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
return stringBuilder.toString();
}
//向Product post对象添加数组
if(model.getCostpersizes()!=null){
对于(int k=0;k代码是正确的。存在空指针字符串。未从GUI界面传递字符串说明。这导致错误
Costpersizes[]值也更改为Costpersizes[k]。
需要传递一个索引,以便api能够区分数组中的行。阅读后我不明白。请在代码中添加更多注释,解释预期和非预期行为。@FailingCoder我编辑了代码。如果仍然很复杂,请告诉我。添加了更多注释
//Adding array to the Product post object
if(model.getCostpersizes() != null ){
for(int k=0; k<model.getCostpersizes().size(); k++){
Productpricemodel pricemodel = model.getCostpersizes().get(k);
entity.addPart("Costpersizes[].SN", new StringBody(pricemodel.getSN()));
entity.addPart("Costpersizes[].ProductID", new StringBody(pricemodel.getProductID()));
entity.addPart("Costpersizes[].AvailableQuantity", new StringBody(pricemodel.getAvailableQuantity()+""));
entity.addPart("Costpersizes[].Barcode", new StringBody(pricemodel.getBarcode()));
entity.addPart("Costpersizes[].Cost", new StringBody(pricemodel.getCost()+""));
entity.addPart("Costpersizes[].Size.ID", new StringBody(pricemodel.getSize().getID()));
entity.addPart("Costpersizes[].Response", new StringBody(pricemodel.getResponse()));
}
}
public class Product
{
public IFormFile Image { get; set; }
public OptionSelect Unit { get; set; }
public string Name { get; set; }
public string Barcode { get; set; } //This is code is used to prove originality of the product
public int StockQuantity { get; set; }
public double UnitCost { get; set; }
public int Discountamt { get; set; }
public string Description { get; set; }
public int CompanyID { get; set; }
// public List<OptionSelect> Producttags { get; set; }
public string Hascolortypes { get; set; }
public List<Productpricemodel> Costpersizes { get; set; }
public string InventorycategoryID { get; set; }
}
public class Productpricemodel
{
[JsonProperty("sn")]
public string SN { get; set; }
[JsonProperty("cost")]
public float Cost { get; set; }
[JsonProperty("productID")]
public string ProductID { get; set; }
[JsonProperty("size")]
public OptionSelect Size { get; set; }
[JsonProperty("availablequantity")]
public double AvailableQuantity { get; set; }
[JsonProperty("barcode")]
public string Barcode { get; set; }
[JsonProperty("response")]
public string Response { get; set; }
}
public List<Productpricemodel> Costpersizes { get; set; }