SpringMVC⾥preHandle⾥的参数究竟是什么意思
今天我的update接⼝⽼是报错,请求和处理⽅法和create⼀模⼀样。
create接⼝:
@RequestMapping(value = "/oftenTraveller/create", method = {RequestMethod.POST})
public ResponseEntity<Response<OftenTravellerDTO>> createOftenTraveller(HttpServletRequest request, HttpServletResponse response, @RequestBody OftenTravellerParam newContent) {
TokenInMemcached token = getTokenInMemcached(request);
String userId = User().getId();
newContent.setOwnerId(userId);
Response<OftenTravellerDTO> resp = new Response<>();
log.debug("create OftenTraveller :{}", newContent);
OftenTravellerDTO dto = ate(newContent);
resp.setResult(Result.SUCCESS_RESULT);
// to dto
update是什么resp.setData(dto);
return new ResponseEntity<>(resp, HttpStatus.CREATED);
}
update接⼝:
@RequestMapping(value = "/oftenTraveller/update/{id}", method = {RequestMethod.POST})
public ResponseEntity<Response<OftenTravellerDTO>> updateOftenTraveller(HttpServletRequest request, HttpServletResponse response, @PathVariable Long id, @RequestBody OftenTravellerParam updateContent) throw Exception{
Response<OftenTravellerDTO> resp = new Response<>();
log.debug("update OftenTraveller id:{} to {}", id, updateContent);
OftenTraveller entity = One(id);
if (entity == null) {
log.warn("OftenTraveller id:{} not found", id);
resp.setResult(Result.FAIL_RESULT);
return new ResponseEntity<>(resp, HttpStatus.BAD_REQUEST);
}
updateContent.setId(id);
try {
} catch (Exception e) {
<(e.getMessage(), e);
throw new Message(),e);
}
oftenTravellerService.update(entity);
// to dto
OftenTravellerDTO dto = DTOUtils.map(entity, OftenTravellerDTO.class);
resp.setData(dto);
resp.setResult(Result.SUCCESS_RESULT);
return new ResponseEntity<>(resp, HttpStatus.OK);
}
程序在每次的request⾥都会被拦截报错:
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String handlerValue = String();
String[] methodStringArray = StringUtils.split(handlerValue);
String methodName = methodStringArray[methodStringArray.length - 1];
String apiName = ShortMethodName(methodName);//在这⼉报错
……
报错内容是空指针;
⽽我把handler⾥⾯的内容打印出来,发现:
create:
public org.springframework.http.ResponseEntity<com.smartprint.biztripmon.dto.Response<com.smartprint.biztripmon.dto.profile.OftenTravellerD TO>> com.smartprint.ller.v1.ateOftenTraveller(javax.servlet.http.HttpServletRequest,javax.servlet.htt p.HttpServletResponse,com.smartprint.biztripmon.dto.profile.OftenTravellerParam)
update:
public org.springframework.http.ResponseEntity<com.smartprint.biztripmon.dto.Response<com.smartprint.biztripmon.dto.profile.OftenTravellerD TO>> com.smartprint.ller.v1.userCenter.OftenCenterController.updateOftenTraveller(javax.servlet.http.HttpServletRequest,javax.servlet.ht tp.HttpServletResponse,java.lang.Long,com.smartprint.biztripmon.dto.profile.OftenTravellerParam) throws java.lang.Exception
联系上⽂:
String apiName = ShortMethodName(methodName);//在这⼉报错
查看了下ShortMethodName(methodName)
public static String getShortMethodName(String fullName) {
int openBracketIdx = fullName.indexOf("(");
String methodNameWithoutParam = fullName.substring(0, openBracketIdx);
String[] arrays = methodNameWithoutParam.split("\\.");
int length = arrays.length;
return arrays[length - 2] + "." + arrays[length - 1];
}
最后的“throws java.lang.Exception”有问题
在controller⾥⾯去掉update后的抛异常的部分,问题解决。
在此,明⽩了。⾥⾯的三个参数:
request : 是指经过spring封装的请求对象, 包含请求地址, 头, 参数, body(流)等信息.
response:是指经过spring封装的响应对象, 包含输⼊流, 响应body类型等信息.
handler,是指controller的@Controller注解下的"完整"⽅法名, 是包含exception等字段信息的.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论