【问题标题】:Hibernate and angular 4+ http get - Parameter value did not match expected typeHibernate 和 Angular 4+ http get - 参数值与预期类型不匹配
【发布时间】:2017-11-21 10:52:51
【问题描述】:

我在后端遇到了非常奇怪的行为。

在我用 Angular 4 编写的前端上,我从后端调用方法,该方法将从特定数据库表中获取特定组织单位的所有员工。另外,我正在调用使用来自后端应用程序的选项填充 MatSelect 的方法。

代码:

后端控制器:

    @RequestMapping(value = "/orgid/{orgid}", produces = "application/json")
public Iterable<Employee> getEmployeeByOrgId(@PathVariable("orgid") String orgid, HttpServletRequest req){
    Iterable<Employee> getEmpByOrgId = eDao.findEmployeesByOrgNameId(orgid);
        return  getEmpByOrgId;
    }

后端 DAO:

  @Query("SELECT new model.Employees(p.id, p.firstName, p.lastName, p.organizationName, p.subOrganizationName, p.mobilephoneNumber, p.telephoneNumber, p.smallImage, p.jobTitle) FROM Employee p WHERE p.orgId = :orgId ORDER by p.lastName")
public Iterable<Employee> findEmployeesByOrgNameId(@Param("orgId") String orgId);

前端服务:

  fetchEmployeesByOrg(org): Observable<Employees[]>{
const url ="employees/orgid/"+org;
return this.http.get<Employees[]>(url);

}

前端组件:

  fetchAllEmployeesByOrgId(org){
this.orgId = org.id.toString();
console.log(typeof this.orgId);
   this.fetch.fetchEmployeesByOrg(this.orgId).subscribe(res => {
      this.EmployeesByOrg = res;
      console.log(this.EmployeesByOrg);
    })
  }

前端html:

  <mat-select placeholder="Choose it">
<mat-option *ngFor="let org of this.orgUnits" [value]="org.value" (click)="fetchAllEmployeesByOrgId(org)">
  {{ org.orgName }}
</mat-option>

当我点击特定选项时,我的网址看起来如何:employees/orgid/166

问题:

我收到java.lang.IllegalArgumentException: Parameter value [166] did not match expected type [java.lang.Integer (n/a)] 如上所述,我期待后端的字符串,而且,我在 url 中提供字符串。

也许是自定义查询内部的问题或其他问题? 任何帮助都会很棒。

【问题讨论】:

  • Employee 表中的 culomn orgId 的类型是什么??
  • 这是一个数字。我有点困惑,因为在 Angular 1.x 中它可以与字符串传递一起使用......
  • @param 参数必须是整数:findEmployeesByOrgNameId(@Param("orgId") Integer orgId),并且在您的控制器中,您可以将值解析为整数:eDao.findEmployeesByOrgNameId(Integer.valueOf(orgid));
  • 是的,它奏效了。将其发布为答案。谢谢!

标签: angular hibernate http-get angular-httpclient


【解决方案1】:

问题是@param arg 被作为String 注入到查询中,因此必须更改方法签名:

findEmployeesByOrgNameId(@Param("orgId") Integer orgId)

如果您将String 传递给您的控制器(您确定可以解析为Integer),您可以将参数转换为Integer,然后调用dao 方法:

eDao.findEmployeesByOrgNameId(Integer.valueOf(orgid));

【讨论】:

    猜你喜欢
    • 2016-07-13
    • 2013-02-20
    • 2019-02-11
    • 2021-08-05
    • 1970-01-01
    • 2018-09-02
    • 2016-04-22
    • 2020-11-29
    • 2015-12-13
    相关资源
    最近更新 更多