关于Jersey整合Spring注入出现NullPointException问题

解决方案如果:

一、web.xml

<servlet>
		<servlet-name>Jersey Web Application</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
		<!--关键是这两行,其中com.example.MyApplication是自定义的一个类-->
		<init-param>
			<param-name>javax.ws.rs.Application</param-name>
			<param-value>com.example.MyApplication</param-value> 
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey Web Application</servlet-name>
		<url-pattern>/webapi/*</url-pattern>
	</servlet-mapping>
package com.example;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

/**
 * Spring HelloWorld Web Application configuration.
 *
 * @author Jakub Podlesak (jakub.podlesak at oracle.com)
 */
public class MyApplication extends ResourceConfig {

    /**
     * Register JAX-RS application components.
     * 如果要和spring要个jersey结合,必须得在jersey 中注册
     */
    public MyApplication() {
        register(RequestContextFilter.class);
        register(SpringRequestResource.class);
    }
import org.springframework.stereotype.Component;

import com.example.emp.EmpService;

@Path("spring-hello")
@Component
public class SpringRequestResource {
	@Resource
	private EmpService empService;
	@GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getHello() {
        return empService.greet("world");
    }

232802_W1EP_1455335.png

已经执行成功,具体为什么要注册,有时间看完源码给大家分享出来。

转载于:https://my.oschina.net/u/1455335/blog/519596

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值