3

我有一个休息网络服务,它从一个FoodItem没有公共空构造函数的类返回对象。但是,它包含另一个不是空参数的构造函数。

我得到了这个例外

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

当我在我的FoodItem类中创建公共空参数构造函数时,异常解决了,我得到了确切的结果。

我的问题: 如何在不添加空构造函数的情况下解决该异常?,在我的业务逻辑中不可能有空构造函数。

提前致谢

编辑 我的类实现了一个接口,也许这些信息可以帮助你。

Edit2 如果你想要我的web.xml、我的类、我的接口或我的网络服务的代码,我准备好了

编辑3

这是网络服务中的代码

@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.

4

2 回答 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.

于 2013-05-16T06:19:48.523 回答
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.

于 2013-05-16T06:19:31.813 回答