这是我的spring.xml,以及maven依赖项和包名:
Package name,spring.xml,Maven dependency
我无法将spring.xml作为代码提供,因为名称空间已转换为链接,因为声誉而无法发布超过2个链接。
spring.xml的下半部分
<bean id="user" class="com.javacodegeeks.enterprise.rest.jersey.UserDetails"
autowire="byName">
<property name="adminName" value="Root"></property>
<property name="name" value="Sparsh"></property>
</bean>
<bean id="officeAddress" class="com.javacodegeeks.enterprise.rest.jersey.Address">
<property name="roomNumber" value="${roomNumber}"></property>
<property name="streetName" value="office"></property>
</bean>
<bean id="homeAddress" class="com.javacodegeeks.enterprise.rest.jersey.Address">
<property name="roomNumber" value="1"></property>
<property name="streetName" value="home"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>
message
</value>
</list>
</property>
</bean>
<bean
class="com.javacodegeeks.enterprise.rest.jersey.DisplayBeanPostProcessor" />
<bean class="com.javacodegeeks.enterprise.rest.jersey.DisplayBeanFactory" />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="variable.properties" />
</bean>
<context:component-scan base-package="com.javacodegeeks.enterprise.rest.jersey" />
一切都很棒但是当我添加
时<context:component-scan base-package="com.javacodegeeks.enterprise.rest.jersey" />
面临错误:
java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.asm.ClassReader.<init>(Unknown Source)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.javacodegeeks.enterprise.rest.jersey.HelloWorldREST.<clinit>(HelloWorldREST.java:122)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:245)
at com.sun.jersey.server.spi.component.ResourceComponentConstructor.construct(ResourceComponentConstructor.java:233)
at com.sun.jersey.server.impl.resource.PerRequestFactory$PerRequest._getInstance(PerRequestFactory.java:182)
at com.sun.jersey.server.impl.resource.PerRequestFactory$AbstractPerRequest.getInstance(PerRequestFactory.java:144)
at com.sun.jersey.server.impl.application.WebApplicationContext.getResource(WebApplicationContext.java:239)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1542)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1473)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733)
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.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:472)
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:947)
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:1009)
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)
HelloWorldRest.java
package com.javacodegeeks.enterprise.rest.jersey;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import net.sf.json.JSONObject;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@Path("/helloworld")
public class HelloWorldREST {
@GET
@Path("/signin/")
public Response responseMsg(@QueryParam("email") String email,
@QueryParam("pass") String pass) {
Session session = factory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();
Long password = (Long) session.createQuery(
"select count(*) from UserDetails where EmailID = '" + email
+ "'and password = '" + pass + "'").uniqueResult();
System.out.println(password);
tx.commit();
session.close();
if (password > 0)
return Response.status(200).entity("success").build();
else
return null;
}
@GET
@Path("/signup/")
public Response responseMsg(@QueryParam("name") String name,
@DefaultValue("Nothing to say") @QueryParam("email") String email,
@QueryParam("date") String date, @QueryParam("pass") String pass) {
Session session = factory.openSession();
org.hibernate.Transaction tx = session.beginTransaction();
UserDetails user = (UserDetails) context.getBean("user");
user.setEmailID(email);
if (name.length() != 0)
user.setName(name);
user.setDate(date);
user.setPass(pass);
session.save(user);
tx.commit();
session.close();
return Response.status(200).entity("success").build();
}
@GET
@Path("/submit/")
@Produces(MediaType.APPLICATION_JSON)
public JSONObject submitValue(@QueryParam("start") int start,
@QueryParam("limit") int limit, @QueryParam("page") int page) {
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Query qry = session.createQuery("from UserDetails");
qry.setFirstResult(start);
qry.setMaxResults(limit);
List<UserDetails> userObject = (List<UserDetails>) qry.list();
JSONObject jObject = new JSONObject();
jObject.put("result", userObject);
jObject.put("success", true);
Long count = (Long) session.createQuery(
"select count(*) from UserDetails").uniqueResult();
jObject.put("totalcount", count);
tx.commit();
session.close();
return jObject;
}
@GET
@Path("/delete/")
public Response hqlTruncate() {
String hql = String.format("delete from UserDetails");
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery(hql);
query.executeUpdate();
tx.commit();
session.close();
return Response.status(200).entity("success").build();
}
static AbstractApplicationContext context;
static SessionFactory factory = null;
static {
Configuration config = new Configuration();
config.configure();
ServiceRegistryBuilder srBuilder = new ServiceRegistryBuilder();
srBuilder.applySettings(config.getProperties());
ServiceRegistry serviceRegistry = srBuilder.buildServiceRegistry();
factory = config.buildSessionFactory(serviceRegistry);
context = new ClassPathXmlApplicationContext("spring.xml");
System.out.println(context.getMessage("greeting", null,
"default message", null));
context.registerShutdownHook();
}
}