解決方案若是:java
1、web.xmlweb
<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"); }
已經執行成功,具體爲何要註冊,有時間看完源碼給你們分享出來。spring