如何使用Spring启动将YML文件中的复杂类型映射到java对象?

时间:2017-07-16 07:19:02

标签: java spring-boot yaml

以YML格式列出复杂对象:

@model IEnumerable<Application.Models.Store>
@using Application.Models
@{
ViewData["Title"] = "Index";
}

<h2>Stores</h2>

<div class="top-buffer"></div>
<div class="panel panel-primary">
<div class="panel-heading panel-head">Stores</div>
<div class="panel-body">
    <div class="btn-group">
        <a id="createEditStoreModal" data-toggle="modal" asp-action="Create" data-target="#modal-action-store"
           class="btn btn-primary">
                <i class="glyphicon glyphicon-plus"></i>  Add Store
            </a>
    </div>
    <div class="top-buffer"></div>
</div>

我需要将此结构映射到java类:

@model Application.Models.ApplicationviewModels.StoreIndexData
@using Application.Models


<form asp-action="Create" role="form">

<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="modal-body form-horizontal">
    <div class="form-group">
        <label asp-for="DepartmentID" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <select asp-for="DepartmentID" class="form-control"
                    asp-items="@(new SelectList(@ViewBag.ListofDepartment,"DepartmentID","DepartmentName"))"></select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-md-2 control-label">District</label>
        <div class="col-md-10">
            <select class="form-control" id="DistrictID" name="DistrictID" asp-for="DistrictID"
                    asp-items="@(new SelectList(@ViewBag.ListofDistrict,"DistrictID","DistrictName"))"></select>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="StoreChainID" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <select asp-for="StoreChainID" class="form-control" asp-items="ViewBag.ChainList"></select>
            <span asp-validation-for="StoreChainID" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="StoreName" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="StoreName" class="form-control" />
            <span asp-validation-for="StoreName" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="StoreAddress" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="StoreAddress" class="form-control" />
            <span asp-validation-for="StoreAddress" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group">
        <label asp-for="StoreArea" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="StoreArea" class="form-control" />
            <span asp-validation-for="StoreArea" class="text-danger"></span>
        </div>
    </div>
    @await Html.PartialAsync("_ModalFooter", new ModalFooter { })
</div>

是否可以使用弹簧启动功能进行映射?如果是的话,我将以感谢为例。

1 个答案:

答案 0 :(得分:1)

根据这个Yaml Tutorial,这是如何假设反序列化yaml文件:

YamlReader reader = new YamlReader(new FileReader("your-yaml.yml"));
Object object = reader.read();
System.out.println(object);
MessageProvider map = (MessageProvider)object;
System.out.println(map.get("error1"));