我正在网上开发所以我有一个ajax函数,它调用一个调用DAO函数的控制器函数(在DB上进行更改)。我在控制器函数中得到了上面的例外..
控制器功能:
@RequestMapping(value="/changeIsPublic", method=RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody boolean changeIsPublic(HttpServletRequest request, Locale locale, Model model, long transactionId, boolean isPublic) {
boolean result = false;
try {
boxDao.changeIsPublicStatus(transactionId, isPublic);
result = true;
} catch (Exception e) {
logger.debug("Failed to publish transaction. transaction ID: " + transactionId + e.getMessage());
}
return result;
}
DAO功能:
public Box changeIsPublicStatus(long id, boolean isPublic) {
Criteria criteria = getCurrentSession().createCriteria(Box.class);
criteria.add(Restrictions.eq("id", id));
Box transaction = (Box) criteria.uniqueResult();
transaction.setIsPublic(isPublic);
return transaction;
}
异常:
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/goblin] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Optional long parameter 'transactionId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.] with root cause
java.lang.IllegalStateException: Optional long parameter 'transactionId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.handleNullValue(AbstractNamedValueMethodArgumentResolver.java:188)
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:94)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:149)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.yes.java.security.AuthenticationFilter.doFilter(AuthenticationFilter.java:105)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source) `
答案 0 :(得分:14)
当我使用Jackson REST Web服务(RESTful Spring Controllers)时出现此错误。问题是我忘记了def insertionsort(list1):
for index in range(1,len(list1)):
value = list1[index]
position = index
while position > 0 and list1[position-1] > value:
list1[position]=list1[position-1]
position = position-1
list1[position] = value
return(list1)
注释,该注释告诉Web服务它应该在哪里接收输入以产生响应,因此它不知道我应该在哪里传递输入。我的修复是:
@PathVariable
答案 1 :(得分:11)
异常消息指导您。将长类型更改为长
答案 2 :(得分:8)
错误几乎是不言自明的:你不能将原语声明为null
,例如:private int myNumber = null;
将无法编译。因此,不要使用long
使用Long
,而应该很高兴。
答案 3 :(得分:4)
注释:
@RequestParam(defaultValue = "0")
。
答案 4 :(得分:0)
What is the difference between long and Long in android code?初学者的一些基本概念
Long是一门课。 long是原始的。这意味着Long可以为null,而long不能为null。 Long可以走到任何需要Object的地方,而Long不会(因为它不是从Object派生的类)。
Java通常会自动将Long转换为long(反之亦然),但是不会将其转换为null(因为long不能为null),并且在需要传递时需要使用Long版本一个类(例如在通用声明中)。
答案 5 :(得分:0)
@Ahmed Tawila-他提到我犯了同样的错误。我忘记在控制器中方法的原始类型之前添加@PathVariable
注释。
代码错误:未在长整型之前定义必需的注释
@RequestMapping(method = RequestMethod.DELETE, value = "/categories/{categoryId}/subcategories/{id}")
public void deleteSubCategory(long id) throws Exception {
subCategoryService.delete(id);
}
正确的代码:添加了注释(@PathVariable
)
@RequestMapping(method = RequestMethod.DELETE, value = "/categories/{categoryId}/subcategories/{id}")
public void deleteSubCategory(@PathVariable long id) throws Exception {
subCategoryService.delete(id);
}
答案 6 :(得分:0)
这有时是由于使用PathParam而不是PathVariable引起的。这可能只是另一个解决方案。我们也可以看看它。在使用JpaRepository接口实现Spring数据jpa时,我遇到了类似的情况。
答案 7 :(得分:0)
就我而言,我在 Controller 的请求正文中缺少@RequestBody
注释!
public View updateView(@RequestBody int id){
}
希望它对某人有帮助!