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

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条回答 默认 最新

  • 久绊A 全栈领域新星创作者 2023-02-12 17:47
    关注
    要整合Jersey2和Spring3来实现HTTP服务获取上下文,首先需要在web.xml文件中配置Jersey2的Servlet容器,然后在web.xml文件中配置Spring的ContextLoaderListener,最后在Jersey2的Servlet容器中配置Spring的ApplicationContext。具体步骤如下:
    1. 在web.xml文件中配置Jersey2的Servlet容器:
    <servlet>
        <servlet-name>Jersey2</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.MyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    2. 在web.xml文件中配置Spring的ContextLoaderListener:
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    3. 在Jersey2的Servlet容器中配置Spring的ApplicationContext:
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    
    
    评论

    报告相同问题?

    悬赏问题

    • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?
    • ¥15 ORBSLAM2框架跑ICL-NUIM数据集
    • ¥15 在我想检测ros是否成功安装时输入roscore出现以下
    • ¥30 老板让我做一个公司的投屏,实时显示日期,时间,安全生产的持续天数,完全没头绪啊
    • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
    • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
    • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
    • ¥20 怎样通过一个网址找到其他同样模版的网址
    • ¥30 XIAO esp32c3 读取FDC2214的数据
    • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信