Autowired is null and does not work with Jersey + Spring

I have a problem figuring out why the entry point in My Jersey RESTful cannot find the Spring Bean that I configure when the application server starts. It continued to receive a NullPointerException after trying from

web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.testing.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Spring -context.xml

<context:annotation-config />
<context:component-scan base-package="com.testing.config, com.testing.repository, com.testing.workflow" />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>

Servlet servlet entry point

@Component
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}/items")
public class UserResource
{
  @Autowired
  private UserWorkFlow userWorkFlow;

  @GET
  public Response getAllItems(@PathParam("userId") String userId)
  {
    // ** NullPointerException here complains about the UserWorkFlow 
    return Response.status(200).entity(userWorkFlow.getItemsFor(userId)).build();
  }
}

Service level

I also tried to create an interface for this, but it did not work.

@Service
public class UserWorkFlow
{
  @Autowired
  private AllItems allItems;

  public List<Item> getItemsFor(String id)
  {
    return allItems.getItemsFor(id);
  }
}

Repository and DAO

@Repository
public class AllItems
{
  @Autowired
  private ItemSql itemSql;

  public List<Item> getItemsFor(String id)
  {
    return itemSql.getAllItemsFor(id);
  }
}

MyBatis Mapper (it has XML associated with it)

public interface UserSql
{
  List<Item> getAllItemsFor(@Param("userId") String userId);
} 

com.sun.jersey org.glassfish.jersey, . , . - , ?

+4
1

, . .

. , . Jersey 2.x Spring XML.

, , ( , pom)

  • jersey-container-servlet: 2.22.1
  • spring-web: 3.2.3.RELEASE
  • commons-logging
  • jersey-spring3: 2.22.1. ( , jersey-spring*4*. ).

, , web.xml

, applicationContext.xml .

Fouth, MyApplication , web.xml.

T, . , Spring, , , , , . , ( ) / .

+3

Source: https://habr.com/ru/post/1616783/


All Articles