整合 spring
jersey2 官方还未正式支持 spring4, 但网上有好多支持方案,折腾了一圈后,还是用了 spring3;
pom 添加以下依赖配置
org.springframework
spring-web
4.3.1.RELEASE
javax.servlet
javax.servlet-api
3.1.0
provided
org.glassfish.jersey.containers
jersey-container-servlet
org.glassfish.jersey.ext
jersey-spring3
添加 JerseyConfig 类
package com.easymylife.app.sys;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(RequestContextFilter.class);
packages("com.easymylife.app.sys");
//register(LoggingFeature.class);
}
}
添加 SpringAnnotationConfig 类
package com.easymylife.app.sys;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Spring configuration to include our services
*
*/
@Configuration
@ComponentScan(basePackageClasses = {})
public class SpringAnnotationConfig {
}
然后在 main 中调用
final JerseyConfig resourceConfig = new JerseyConfig();
resourceConfig.property("contextConfig", new AnnotationConfigApplicationContext(SpringAnnotationConfig.class));
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), resourceConfig, false);
就可以通过 @Autowired 注解进行注入
@Singleton
@Path("spring-resource")
@Service
public class SpringRequestResource {
AtomicInteger counter = new AtomicInteger();
/* @Autowired
private GoodbyeService myGoodbyeService;*/
@Autowired
private ISaySthService saySthService;
@GET
@Path("saysth")
@Produces(MediaType.TEXT_PLAIN)
public String getSaySth(){
return this.saySthService.hello("Grissom");
}
@GET
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
public List getUsers(){
return this.saySthService.getUsers();
}
}
整合 hibernate
pom 配置
mysql
mysql-connector-java
5.1.21
org.hibernate
hibernate-core
5.2.10.Final
org.hibernate
hibernate-validator
5.4.0.Final
如果通过 JPA 方式打开数据库 Session, 则在 main/resources/META-INF/ 下添加 persistence.xml 配置,
访问数据库代码
EntityManagerFactory factory = Persistence.createEntityManagerFactory("mysqlJPA");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("From SysUser");
List user = query.getResultList();
如果直接用 hibernate 创建 session, 则在 mian/resources 下添加 hibernate.cfg.xml
com.mysql.jdbc.Driver
usernae
pwd
jdbc:mysql://www.yourdomain.cn:3306/dbname
org.hibernate.dialect.MySQLDialect
true
true
代码
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
sessionFactory = configuration.buildSessionFactory();
Session session = getInstance().sessionFactory.openSession();
Query query = session.createQuery("from SysUser");
List employees = query.list();
session.close();
return employees;
整合 log4j2
pom
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
main/resources 下添加 log4j2.xml
C:\\logs
error
info
debug
我完整的 pom.xml
4.0.0
com.easymylife.app
sys
jar
0.0.1-SNAPSHOT
sys
org.glassfish.jersey
jersey-bom
${jersey.version}
pom
import
org.glassfish.jersey.containers
jersey-container-grizzly2-http
org.glassfish.jersey.media
jersey-media-json-jackson
2.25.1
junit
junit
4.9
test
org.glassfish.jersey.core
jersey-client
test
org.glassfish.jersey.core
jersey-client
2.25.1
provided
asm
asm
3.3.1
org.springframework
spring-web
${spring.version}
javax.servlet
javax.servlet-api
3.1.0
provided
org.glassfish.jersey.containers
jersey-container-servlet
org.glassfish.jersey.ext
jersey-spring3
mysql
mysql-connector-java
5.1.21
org.hibernate
hibernate-core
5.2.10.Final
org.hibernate
hibernate-validator
5.4.0.Final
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
org.apache.maven.plugins
maven-compiler-plugin
2.5.1
true
1.6
org.codehaus.mojo
exec-maven-plugin
1.2.1
java
com.easymylife.app.sys.Main
4.3.1.RELEASE
2.25.1
UTF-8