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 = token.getUser().getId();
        newContent.setOwnerId(userId);


        Response<OftenTravellerDTO> resp = new Response<>();
        log.debug("create OftenTraveller :{}", newContent);

        OftenTravellerDTO dto = oftenTravellerService.create(newContent);

        resp.setResult(Result.SUCCESS_RESULT);
        // to dto
        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 = oftenTravellerService.getOne(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 {
            BeanUtils.copyProperties(entity, updateContent);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new BusinessException(e.getMessage(),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 = handler.toString();
        String[] methodStringArray = StringUtils.split(handlerValue);
        String methodName = methodStringArray[methodStringArray.length - 1];
        String apiName = ApiStatHelper.getShortMethodName(methodName);//在這兒報錯
        ……

報錯內容是空指針;
而我把handler裏面的內容打印出來,發現:
create:

public org.springframework.http.ResponseEntity<com.smartprint.biztrip.common.dto.Response<com.smartprint.biztrip.common.dto.profile.OftenTravellerDTO>> com.smartprint.biztrip.api.controller.v1.userCenter.OftenCenterController.createOftenTraveller(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,com.smartprint.biztrip.common.dto.profile.OftenTravellerParam)

update:

public org.springframework.http.ResponseEntity<com.smartprint.biztrip.common.dto.Response<com.smartprint.biztrip.common.dto.profile.OftenTravellerDTO>> com.smartprint.biztrip.api.controller.v1.userCenter.OftenCenterController.updateOftenTraveller(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse,java.lang.Long,com.smartprint.biztrip.common.dto.profile.OftenTravellerParam) throws java.lang.Exception

聯繫上文:

String apiName = ApiStatHelper.getShortMethodName(methodName);//在這兒報錯

查看了下ApiStatHelper.getShortMethodName(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 : 是指
response:是指
handler,是指controller的@Controller註解下的整個方法名

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章