您好,我正在尝试从jee应用程序到asp.net使用其余的Web服务 当我复制url时,它可以正常工作,但是当我从应用程序中删除该url时,它会专门更改日期。
erorr的示例:
22:03:32,629 WARN [org.jboss.resteasy.core.ExceptionHandler](默认 任务69)无法执行:javax.ws.rs.NotFoundException:无法 查找完整路径的资源: http://localhost:18080/PiDev-web/rest/absence/creates/aaaqwqwq/07/11/2018%2000:00:00/07/11/2018%2000:00:00
[JEE应用]
这是我的服务:
@Override
public void createAbsence(Absence e) {
EmployeService ee =new EmployeService();
ConnectedUser emp = em.find(ConnectedUser.class, 1);
System.out.println("Service Done 0 !!!");
e.setIdEmploye(emp.getId());
e.setLogin(emp.getLogin());
e.setJustifie(false);
//e.setEmploye(UserConnected);
em.persist(e);
System.out.println("Service Done !!!");
}
和myBean:
@POST
@Path("/creates/{cause}/{dd}/{df}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response serviceCreates(@PathParam(value = "cause") String cause,@PathParam(value = "dd") String dd,@PathParam(value = "df") String df) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dated=new Date();
Date datef=new Date();
try {
dated = dateFormat.parse(dd);
datef = dateFormat.parse(df);
System.out.println(dated);
} catch (ParseException e) {
e.printStackTrace();
}
return Response.status(Status.ACCEPTED).entity(absenceService.createAbsence(new Absence(cause,dated,datef))).build();
}
[Asp.Net应用] 控制器:
[HttpPost]
public ActionResult Create(absence abs)
{
try
{
HttpClient Client = new System.Net.Http.HttpClient();
Console.WriteLine(abs.dateDebut);
Client.BaseAddress = new Uri("http://localhost:18080/PiDev-web/rest/absence/");
Client.PostAsJsonAsync<absence>("creates/" + abs.cause + "/" + abs.dateDebut + "/" + abs.dateFin, abs);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
和我的观点:
@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/backLayout.cshtml";
}
@model ProjectWeb.Models.absence
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Add Absence</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.cause, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.cause, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.cause, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.dateDebut, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.dateDebut, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.dateDebut, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.dateFin, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.dateFin, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.dateFin, "", new { @class = "t-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我不熟悉日期格式,所以如果您熟悉请帮助我
答案 0 :(得分:0)
URL中使用斜杠分隔年,月和日的问题。实际上,URL使用斜杠来分隔路径的一部分,因此除了路径的单独元素外,您不必使用斜杠。 对于发送日期,最好使用ISO格式。长话短说,您将日期从最大到最小写出,并使用破折号(year-month-dayThour:minutes:seconds)示例将它们分开
date time 2018-11-07T22:30:33
only date 2018-11-07
像这样重写您的资源:
@POST
@Path("/creates/{cause}/{dd}/{df}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response serviceCreates(@PathParam(value = "cause") String cause,@PathParam(value = "dd") LocalDate dd,@PathParam(value = "df") LocalDate df) {
return Response.status(Status.ACCEPTED).entity(absenceService.createAbsence(new Absence(cause,dd,df))).build();
}
并尝试调用:
http://localhost:18080/PiDev-web/rest/absence/creates/aaaqwqwq/2018-11-07/2018-11-07