飞火流星02027 2021-06-01 13:00 采纳率: 0%
浏览 44

jersey2与spring3在java程序中整合实现http服务获取上下文问题

问题: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();
			}
		}
    }
}
  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 SpringBoot项目启动报错:No active profile set, falling back to default profiles:default,如何解决?
      • ¥15 部署confluence设置数据库这个步骤时,提示必须配置utf8 作为默认字符集
      • ¥15 如何脱离Photoshop实现ps脚本的渲染
      • ¥15 丙类高频放大器匹配网络
      • ¥15 Pytorch框架nn.RNN训练时反向传播报错
      • ¥60 添加:普通链接二维码,的时候,总是提示校验文件检查失败
      • ¥15 react的使用问题
      • ¥150 关于#算法#的问题,如何解决?动态平衡算法实现
      • ¥20 苹果手机开启蓝牙和Wi-Fi 按钮后黑客是否可以入侵该手机?
      • ¥15 关于#hadoop#的问题:1.为什么我这里会显示中午数字2.为什么我会连接失败打不开local host