org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是java.lang.NumberFormatException:null

时间:2015-04-23 15:42:47

标签: spring jsp spring-mvc full-text-search spring-data-mongodb

我尝试用包含模型所有属性的spring数据mongodb进行全文搜索。

这个型号:

@Document
public class Offre {
@Id
private String id;
@TextIndexed
private String titreOffre;
@TextIndexed
private String descriptionOffre;
@TextIndexed
private String date;
@TextIndexed
private String lieu;
}

存储库

public interface OffreRepository extends MongoRepository<Offre, String>,     OffreRepositoryCustom,PagingAndSortingRepository<Offre, String> {
Offre findById(String id);
List<Offre> findAll();
}

public interface OffreRepositoryCustom {
public List<Offre> findFullTextSearch(String term);
}

public class OffreRepositoryImpl implements OffreRepositoryCustom{
@Override
public List<Offre> findFullTextSearch(String term){
Query query2 = new Query();
query2.addCriteria(Criteria.where("titreOffre").regex(term));
return mongoTemplate.find(query2, Offre.class);
}
}

这项服务:

public class OffreService {
@Autowired
private OffreRepository offreRepository;
private OffreMapper mapper = new OffreMapper();
@Autowired
MongoTemplate template;
private Logger logger = Logger.getLogger(UserService.class);
public List<OffreUi> findFullTextSearch(String term) {
return mapper.toUIBean(offreRepository.findFullTextSearch(term));
}
}

这个控制器

@Controller
@RequestMapping("/offres")
public class OffreController {
@Autowired
private OffreService service;
private Logger logger = Logger.getLogger(OffreController.class);
private DataTableResponseMap<OffreUi> offreResponseMap = new DataTableResponseMap<OffreUi>();
@RequestMapping (value = "/fulltext/{searchTerm}", method = RequestMethod.GET)
public String getfullPage(@RequestParam(value = "searchTerm", required = false) String searchTerm,Model model) {
model.addAttribute("offres", service.findFullTextSearch(searchTerm));
return "offre/fulltext";
}
@RequestMapping(value = "/fulltext")
public @ResponseBody
Map<String, ? extends Object> fulltext(
@RequestParam(required = false) String iDisplayStart,
@RequestParam(required = false) String iDisplayLength,
@RequestParam(value = "searchTerm", required = false) String searchTerm,
@RequestParam(required = false) String iSortingCols) {
int pageLength = Integer.parseInt(iDisplayLength);
int startPage = Integer.parseInt(iDisplayStart) / pageLength;
Pageable pageable = new PageRequest(startPage, pageLength);
Page<OffreUi> offres = (Page<OffreUi>) service.findFullTextSearch(searchTerm);
return offreResponseMap.mapOK(offres.getContent(),offres.getTotalElements());
}
}    

我的观点: search.jsp的:

<html lang="en">
<jsp:include page="../menu/header.jsp" />
<body>
<jsp:include page="../menu/topMenu.jsp" />
<div>
<form  method="get"action="<c:url value='/offres/fulltext?searchTerm=${searchTerm}'/>">
<input type="text" name="searchTerm" id="searchTerm" value="searchTerm"/>
<input class="btn btn-primary" type="submit" value="Save">
</form>
</div>
</body>
</html>

和fulltextsearch.jsp

<html lang="en">
<jsp:include page="../menu/header.jsp" />
<body>
<jsp:include page="../menu/topMenu.jsp" />
<div class="container-fluid">
<div class="row-fluid">
<div class="span10">
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example" >
<thead>
<tr>
<th width="20%">Titre Offre</th>
<th width="25%">Description Offre</th>
<th width="15%">Date</th>
<th width="15%">Site Entreprise</th>
<th width="15%">Logo Entreprise</th>
<th width="15%">Lieu</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5" class="dataTables_empty">Loading data from server</td>
</tr>
</tbody>    
<tfoot>
<tr>
<th>Titre Offre</th>
<th>Description Offre</th>
th>Date</th>
<th>Site Entreprise</th>
<th>Site Entreprise</th>
<th>Lieu</th>
</tr>
</tfoot>
</table>
</div><!--/span-->
<jsp:include page="../menu/rightMenu.jsp" />
</div><!--/row-->
<hr>
<footer>
<p>&copy; Faycel Gsouma</p>
</footer>
</div><!--/.fluid-container-->          
<jsp:include page="../menu/includeScripts.jsp" />
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "<c:url value="/offres/fulltext?searchTerm=${searchTerm}"/>",
"fnServerData": function ( sSource, aoData, fnCallback ) {
$.ajax( {
"dataType": 'json',
"type": "GET",
"url": sSource,
"data": aoData,
"success": fnCallback
} );
},
"aoColumns": [
{ "mData": "titreOffre" },
{ "mData": "descriptionOffre" },
{ "mData": "date" },
{ "mData": "siteEntreprise" },
{ "mData": "logoEntreprise" },
{ "mData": "lieu" },           
]
} );
} );
</script>
</body>
</html>    

,我得到的错误

exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NumberFormatException: null
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:943)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
cause mère

java.lang.NumberFormatException: null
    java.lang.Integer.parseInt(Integer.java:454)
    java.lang.Integer.parseInt(Integer.java:527)
    org.sab.sampleqa.web.controller.user.OffreController.fulltext(OffreController.java:185)
    org.sab.sampleqa.web.controller.user.OffreController$$FastClassByCGLIB$$46319c43.invoke(<generated>)
    org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
    org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:713)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    org.springframework.aop.interceptor.CustomizableTraceInterceptor.invokeUnderTrace(CustomizableTraceInterceptor.java:257)
    org.springframework.aop.interceptor.AbstractTraceInterceptor.invoke(AbstractTraceInterceptor.java:112)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:646)
    org.sab.sampleqa.web.controller.user.OffreController$$EnhancerByCGLIB$$e7945c7b.fulltext(<generated>)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/8.0.18.

1 个答案:

答案 0 :(得分:0)

您的参数是可选字符串:

@RequestParam(required = false) String iDisplayStart,
@RequestParam(required = false) String iDisplayLength,

然后解析:

int pageLength = Integer.parseInt(iDisplayLength);

导致NPE。

您应首先检查null:

int pageLength = null == iDisplayLength
                      ? 10
                      : Integer.parseInt(iDisplayLength);

或使用defaultValue @RequestParam参数,例如

@RequestParam(required = false, defaultValue="0") String iDisplayStart,
@RequestParam(required = false, defaultValue="10") String iDisplayLength,