问题:jersey2与spring3在java程序中整合实现http服务(不是部署在web容器中的web应用)
客户端通过http://localhost:8087/prov43/nmggz/dic/update/BAB063
调用 dicService的 updateDict 方法, 但dicService为null, 没有spring上下文
加入jersey-spring3-2.6.jar后,启动服务就报,找不到applicationContext.xml配置文件,我的配置文件在
“D:\\省svn\\新一代中间业务平台\\开发或操作手册\\省平台\\cpabprov\\exf\\config\\spring.xml” -- 见下面的单元测试类
import java.net.URI;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("restServer")
public class RESTServer {
/** 服务主机ip **/
@Value("${prov43.httpserver.base_uri}")
public String base_uri;
public HttpServer startServer() {
final ResourceConfig rc = new ResourceConfig().packages("com.psbc.pfpj.prov43");
System.out.println("###################################");
System.out.println("启动服务......");
System.out.println("base_uri:"+base_uri);
return GrizzlyHttpServerFactory.createHttpServer( URI.create(base_uri), rc);
}
public String getBase_uri() {
return base_uri;
}
public void setBase_uri(String base_uri) {
this.base_uri = base_uri;
}
}
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.psbc.pfpj.cpabprov.core.protocol.ZJYWResp;
import com.psbc.pfpj.prov43.nmggz.DicService;
import com.psbc.pfpj.prov43.nmggz.entity.DicDO;
@Path("/nmggz/dic/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Component
public class Dic2Service {
@Autowired
private DicService dicService;
@POST
@Path("/update/{dictType}")
public ZJYWResp updateDict( @PathParam("dictType") String dictType, DicDO dicDO) throws Exception{
System.out.println("##### ########################################");
dicService.updateDict( dicDO.getOpeCd(), dictType );
ZJYWResp aidmResp = new ZJYWResp(true, "更新字典数据成功!dictType:"+dictType);
return aidmResp;
}
}
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.psbc.pfpj.cpabprov.core.server.RESTServer;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:D:\\XXXXX\\spring.xml"})
@Slf4j
public class RESTServerTest {
@Autowired
private RESTServer restServer;
@Test
public void testStartServer() {
log.info("正在启动 testStartServer....");
restServer.startServer();
log.info("启动完成!");
// 保证服务一直可用
while (true) {
try {
Thread.sleep(24 * 60 * 60 * 1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}