顾名思义, @PathVariable和@RequestParam,分别是从路径里面去获取变量,也就是把路径当做变量,后者是从请求里面获取参数。
1.@RequestParam :是从请求里面获取参数
请求的url: http://localhost:8080/jfly.openapi/pad/sulfuCheckInfoByMachineNo/v1?machineNo=ECCM001
其中"required = false" 是设置请求参数为非必须
@RequestMapping (value = "/sulfuCheckInfoByMachineNo/v1" , method = RequestMethod. GET )
@ResponseBody
public String I_MES_PAD_16( @RequestParam (value = "machineNo" , required = false ) String machineNo ) {
if ( machineNo != null && ! "" .equals( machineNo )) {
IPadService service = Application. getClassBean (IPadService. class );
List<MesSulfuCheckInfo> infoList = service .findSulfuCheckInfoByMachineNo( machineNo );
if ( infoList != null && ! infoList .isEmpty()) {
jsonString = JSONUtil. getJSONString ( new JSONData( infoList ));
} else {
jsonString = JSONUtil. getErrorJSONString ( "未查询到该机台号硫化信息" );
}
return jsonString ;
} else {
jsonString = JSONUtil. getErrorJSONString ( "机台编号不能为空" );
return jsonString ;
}
}
2.@PathVariable :从路径里面去获取变量
请求的url: http://localhost:8080/jfly.openapi/pad/sulfuCheckInfoByMachineNo1/v1/ECCM001
@RequestMapping (value = "/sulfuCheckInfoByMachineNo1/v1/{machineNo}" , method = RequestMethod. GET )
@ResponseBody
public String I_MES_PAD_18( @PathVariable ( "machineNo" ) String machineNo ) {
if ( machineNo != null && ! "" .equals( machineNo )) {
IPadService service = Application. getClassBean (IPadService. class );
List<MesSulfuCheckInfo> infoList = service .findSulfuCheckInfoByMachineNo( machineNo );
if ( infoList != null && ! infoList .isEmpty()) {
jsonString = JSONUtil. getJSONString ( new JSONData( infoList ));
} else {
jsonString = JSONUtil. getErrorJSONString ( "未查询到该机台号硫化信息" );
}
return jsonString ;
} else {
jsonString = JSONUtil. getErrorJSONString ( "机台编号不能为空" );
return jsonString ;
}
}
原文链接:https://blog.csdn.net/sinat_29774479/article/details/79697383