【发布时间】: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