3

I have a rest web service, It returns objects from a class FoodItem which doesn't have public empty constructor. However, It contains another constructors which aren't empty parameters.

I got this exception

May 15, 2013 11:06:50 PM com.sun.jersey.spi.container.ContainerResponse logException SEVERE: Mapped exception to response: 500 (Internal Server Error) javax.ws.rs.WebApplicationException: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions com.mypackage.eattel.food.FoodItemImpl does not have a no-arg default constructor. this problem is related to the following location: at com.mypackage.eattel.food.FoodItemImpl at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:159) at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306) at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1479) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1391) at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1381) at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716) at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions com.mypackage.eattel.food.FoodItemImpl does not have a no-arg default constructor. this problem is related to the following location: at com.mypackage.eattel.food.FoodItemImpl at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source) at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at javax.xml.bind.ContextFinder.newInstance(Unknown Source) at javax.xml.bind.ContextFinder.find(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at javax.xml.bind.JAXBContext.newInstance(Unknown Source) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getStoredJAXBContext(AbstractJAXBProvider.java:194) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getJAXBContext(AbstractJAXBProvider.java:187) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:165) at com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getMarshaller(AbstractJAXBProvider.java:144) at com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo(AbstractRootElementProvider.java:151) ... 24 more 

when I make public empty parameter constructor in my FoodItem class, the exception solved and I got the exact results.

My question: How to solve that exception without adding empty constructor?, It is impossible in my business logic to have empty constructor.

Thanks in advance

Edit My class implements an Interface, Maybe this information could help you.

Edit2 If you want the code for my web.xml, my classes, my interfaces or my webservice I am ready

Edit3

This is the code in the web service

@GET @Produces(MediaType.TEXT_XML) public FoodItem getRestarantsFordBrowser() { return FoodItemImpl.getInstance(1); } 

so as you see, I am calling Static method.

The code of the static method is:

public static FoodItem getInstance(int ID) { return new FoodItemImpl(ID); } 

and this constructor is PRIVATE, so I don't need a public constructor to create objects and in my web service I am calling method static so I don't have to create object explicity.

2
  • Is the constructor with argument calling no-arg constructor ? Commented May 16, 2013 at 6:54
  • @Santosh no no, it is not Commented May 16, 2013 at 7:17

2 Answers 2

1

In Java, if you don't write a constructor, default constructor will be added by the java run time and instances will be initialized with default values. But if you add a constructor in your class then JVM does not add the default constructor and you should add the default constructor. The reason behind, why JVM adds a default constructor when there is no construcor defined is that, if it does not then you will not be able to create an object of that class. But if you have added a constructor, then JVM assumes that you have one and using that one you should be able to create objects.

Sign up to request clarification or add additional context in comments.

3 Comments

I don't have to use public constructor to create an object, check my edit please
Your REST server is trying to instantiate FoodItemImpl with no argument constructor and as you have not added in your class so it is complining.
why does the REST trying to instantiate a new object from FoodItemImpl ????? I didn't request that
0

This is typical use of reflection in Java and it's being followed by many other frameworks as well which uses reflection to construct objects.

This is how in general POJO or Java beans are constructed (default):

Call default empty constructor of an class, it must be public

Call setters on available fields to set field values

Springs allows various ways to construct objects, in your case you can try using Jersey+Spring for using constructor with parameter approach.

1 Comment

I don't have to use public constructor to create an object, check my edit please

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.