1

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

在我用 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>

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

问题:

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

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

4

1 回答 1

1

The problem is that the @param arg is injected as a String into the query, so the method signature must be changed :

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

and if you passe a String to you controller (that you are sure is parsable to Integer) you can convert the param to an Integer then call the dao method :

eDao.findEmployeesByOrgNameId(Integer.valueOf(orgid));
于 2017-11-21T11:20:49.100 回答