25

Hi i'm developing on web so i have an ajax function which calling to a controller function which calling to a DAO function (to make changes on DB). I'm getting the exception above in the controller function..

controller function:

@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 function:

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;
}

exception:

    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)    `
Erez
  • 502
  • 3
  • 6
  • 17
  • 2
    Did you do what the error message suggests and declare `transactionId` to be a `Long`? You probably should declare `isPublic` to be a `Boolean` while you're at it. – Ted Hopp Jun 01 '14 at 07:26

9 Answers9

45

I got this error when I was working with Jackson REST web services (RESTful Spring Controllers). The problem was that I forgot the @PathVariable annotation which tells the web service where it should receive your input to produce the response so it did not know where I should be passing my input. My fix was:

@RequestMapping(value = "/supplier/{supplierId}")
public List<PurchaseInvoice> getPurchaseInvoicesBySupplierId(@PathVariable int supplierId) {
    return purchaseInvoiceService.getPurchaseInvoicesBySupplierId(supplierId);
}
Ahmed Tawila
  • 988
  • 2
  • 13
  • 20
  • 1
    This helped me. I had it as @ PathParam but obviously needed to be @ PathVariable – JamWill Mar 03 '20 at 12:49
  • This should be marked as correct answer ,as the answer marked to be correct will only resolve the error but wont get the work done as per the code provided – Austine Gwa Jun 17 '20 at 10:49
20

The error is pretty much self explanatory: you can't declare a primitive to be null,
for example: private int myNumber = null; will not compile. So instead of using long use Long and you should be good to go.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
14

Exception message guides you. Change long type to Long

gefrag
  • 352
  • 2
  • 14
11

Annotate with: @RequestParam(defaultValue = "0").

softwarevamp
  • 827
  • 10
  • 14
  • This is what I was looking into, but expected spring could have handle this primitive scenario. – Ram Nov 17 '20 at 05:19
  • This works for Kotlin, tks. And in kotlin the type was Long – Hinotori Mar 14 '21 at 03:43
  • This is what I was looking for. I was hoping spring would pick up Kotlin's default value but it does not. To be more precise, it picks it up to define if the field is required, not to get the default value. – Crystark Feb 08 '23 at 10:27
6

@Ahmed Tawila - as he mentioned I did the same mistake. I forgot to add @PathVariable annotation before the primitive type for the method in controller.

Incorrect Code: Required annotation is not defined before long primitive type

@RequestMapping(method = RequestMethod.DELETE, value = "/categories/{categoryId}/subcategories/{id}")
public void deleteSubCategory(long id) throws Exception {
    subCategoryService.delete(id);
}

Correct Code: Annotation added(@PathVariable)

@RequestMapping(method = RequestMethod.DELETE, value = "/categories/{categoryId}/subcategories/{id}")
public void deleteSubCategory(@PathVariable long id) throws Exception {
    subCategoryService.delete(id);
}
Vitalii Dmitriev
  • 739
  • 1
  • 8
  • 18
Prabhakar
  • 237
  • 1
  • 3
  • 10
2

This is sometime caused by using PathParam instead of PathVariable. this could be just another solution. We can take a look at it as well. I faced similar situation while implementing Spring data jpa with JpaRepository interface.

R acharya
  • 31
  • 4
1

In my case, I was missing @RequestBody Annotation in the request body in Controller!

public View updateView(@RequestBody int id){
}

Hope it helps someone!

Kanagalingam
  • 2,096
  • 5
  • 23
  • 40
0

Some basic concept for beginner from What is the difference between long and Long in android code?

Long is a class. long is a primitive. That means Long can be null, where long can't. Long can go anywhere that takes an Object, long can't (since it isn't a class it doesn't derive from Object).

Java will usually translate a Long into a long automatically (and vice versa), but won't for nulls (since a long can't be a null), and you need to use the Long version when you need to pass a class (such as in a generic declaration).

Community
  • 1
  • 1
Frank.Chang
  • 883
  • 1
  • 10
  • 10
0

In my case:

@RequestParam(name = "status", required = false) Integer status

required Param is false so the status need to available to set null in it

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/29882943) – Muhammedogz Sep 21 '21 at 23:11