我目前正在从我创建的REST服务中读取数据。返回的数据为<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="col-sm-2 control-label" for="radioo">Commission type <b style="color:red;">*</b></label>
<div class="col-lg-10" required>
<label class="custom-control custom-control-primary custom-radio">
<input class="custom-control-input" type="radio" name="comission_type" value="0" checked="checked">
<span class="custom-control-indicator"></span>
<span class="custom-control-label">Fixed price</span>
</label>
<label class="custom-control custom-control-primary custom-radio">
<input class="custom-control-input" type="radio" name="comission_type" value="1">
<span class="custom-control-indicator"></span>
<span class="custom-control-label">Percentage wise</span>
</label>
<label class="custom-control custom-control-primary custom-radio">
<input class="custom-control-input" type="radio" name="comission_type" value="2">
<span class="custom-control-indicator"></span>
<span class="custom-control-label">Monthly</span>
</label>
<label class="custom-control custom-control-primary custom-radio">
<input class="custom-control-input" type="radio" name="comission_type" value="3">
<span class="custom-control-indicator"></span>
<span class="custom-control-label">Yearly</span>
</label>
</div>
<form name="fixedForm">
<div id="fixPerDiv" style="display:block;">
<div class="form-group">
<div class="col-lg-11 col-lg-offset-1">
<div class="table-responsive">
<table class="table" id='commision_tbl'>
<tr>
<td width='20%'>Start price</td>
<td width='20%'>End price</td>
<td width='20%'>Start date</td>
<td width='20%'>End date</td>
<td width='20%'>Comission</td>
<td> </td>
</tr>
<tr>
<td>
<input type="number" name="commissions_start_price[]" class="form-control" value="" placeholder="Start Price" required="required" />
</td>
<td>
<input type="number" name="commissions_end_price[]" class="form-control" value="" placeholder="End Price" required="required" />
</td>
<td>
<div class="input-with-icon">
<input type="text" data-provide="datepicker" value="" data-date-today-highlight="true" name="start_date[]" class="form-control" placeholder="Start date" required="required" /><span class="icon icon-calendar input-icon"></span></div>
</td>
<td>
<div class="input-with-icon">
<input type="text" data-provide="datepicker" value="" data-date-today-highlight="true" name="end_date[]" class="form-control" placeholder="End date" required="required" /><span class="icon icon-calendar input-icon"></span></div>
</td>
<td>
<input type="number" name="commissions_amount[]" class="form-control" value="" placeholder="Commision price" required="required" />
</td>
<td> </td>
</tr>
<tr>
<td colspan="6" align="center">
<input type="button" value="Add More" id="price_addmorebtn" class="btn btn-outline-info">
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</form>
<form name="monYearForm">
<div id="monYearDiv" style="display:none;">
<div class="form-group">
<div class="col-lg-11 col-lg-offset-1">
<div class="table-responsive">
<table class="table" id='commision_tb2'>
<tr>
<td width='30%'>Start date</td>
<td width='30%'>End date</td>
<td width='30%'>Comission</td>
<td> </td>
</tr>
<tr>
<td>
<div class="input-with-icon">
<input type="text" data-provide="datepicker" data-date-today-highlight="true" name="start_date[]" class="form-control" placeholder="Start date" required="required" /><span class="icon icon-calendar input-icon"></span></div>
</td>
<td>
<div class="input-with-icon">
<input type="text" data-provide="datepicker" data-date-today-highlight="true" name="end_date[]" class="form-control" placeholder="End date" required="required" /><span class="icon icon-calendar input-icon"></span></div>
</td>
<td>
<input type="number" name="commissions_amount[]" class="form-control" placeholder="Commision price" required="required" />
</td>
<td> </td>
</tr>
<tr>
<td colspan="4" align="center">
<input type="button" value="Add More" id="price_addmore" class="btn btn-outline-info">
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</form>
。列表中填充了YAML文件内容,这些内容被提取到地图中,然后提取到列表中。
当get调用返回一些内容(即List对象中的内容)时,我的代码完美无缺。当没有返回任何内容时,我得到一个java.net.UnknownServiceException:没有内容类型
List用于填充HTML选择器。如果没有返回任何内容,则不会出现任何选择器。我目前通过捕获异常并继续AJAX调用来完成此工作,如下所示。我只是想知道是否有更好的方法(比如检查返回的InputStream中是否有内容)。
List<String>
编辑:我觉得我的错误可能源于当get方法找不到索引时地图对象返回的内容。所以这里通常是我的REST服务正在做的事情(注意URL请求中给出变量“role”):
URL url = new URL("myurl");
HttpURLConnection request = (HttpURLConnection) url.openconnection();
request.connect();
try{
//parse
JsonParser jp = new.JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonArray unsanitizedMembers = root.getAsJsonArray();
//remove quotes from input
List<String> members = new ArrayList<String>();
for(int i=0; i<unsanitizedMembers.size(); i++)
members.add(unsanitizedMembers.get(i).getAsString());
//add to thymeleaf model
model.addAttributes("members", members);
}
catch(UnknownServiceException e){
//return nothing if given list is empty
return "fragments :: memberResults";
}
return "fragments :: memberResults";
答案 0 :(得分:0)
检查内容是否为null
。也许是这样的:
if (request.getContent() != null)
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));