None or multiple beans found in Spring context for type class ...myPackageHere..., skipping the type
Asked Answered
P

2

7

My Jersey Java classes looks like this:

@Component
...class...MyComponent...extends myParent

And in the parent class:

...abstract class myParent...

  @Autowired
  SomeBean myAutowiredBean

I get some strange warnings when my server starts about:

Nov 14, 2014 10:41:23 AM org.glassfish.jersey.server.spring.SpringComponentProvider bind SEVERE: None or multiple beans found in Spring context for type class ...myPackageAndClassHere..., skipping the type.

Despite the warning message everything works as intended. Every class extending the parent has this warning. Has this to do with the abstract nature of the parent class ?

I found this page where someone else has a similar issue, is this a jersey/spring problem ? I use Jersey 2.11 and Spring 3.2.3.

Polygamist answered 14/11, 2014 at 10:57 Comment(0)
P
4

I faced the same problem. My REST resources responded correctly, but I got the same SEVERE Message. Actually, if trying to inject some of my beans into some of the REST service, I got NullPointerException on any bean injected call.

What happened is that jersey initialized the rest services but did not initialize my spring beans (Let me say...correctly, I had to!!)

I solved just making spring aware of my bean :)

Just add context:component-scan base-package="myPackage"

The warning (and NullPointers) disappeared!

Pouter answered 29/6, 2015 at 21:18 Comment(1)
Sorry, dumb question, where would one add such a line?Overstudy
O
0

I was also facing same issue such as "None or multiple beans found in Spring context for type class com.ilearn.jersey.resource.HealthCheckResource, skipping the type."

This error occurred due to @RestController annotation applied on resource classes. Jersey Framework don't need @RestController annotation on controller classes instead of you can use @javax.annotation.Resource. Even it works without @Resource annotation as well.

You need to add you Controller class to register in the JerseyConfig as give example.


*** 1) Your Controller/Resource class *** 

package com.ilearn.jersey.resource;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Path("/health-check")
@Resource
public class HealthCheckResource {

    private static final Logger logger = LogManager.getLogger(HealthCheckResource.class);
    private static AtomicInteger counter = new AtomicInteger(0);

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response healthCheck() throws JsonProcessingException {
        if(counter.incrementAndGet() == 10) {
            logger.debug("i-learn application is up, Date: {}", LocalDateTime.now());
            counter.set(0);
        }
        return Response.ok(getStatus()).build();
    }

    private String getStatus() throws JsonProcessingException {
        final Map<String, String> serverStatusMap = new ConcurrentHashMap<>();
        serverStatusMap.put("i-learn", "Running");
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(serverStatusMap);
    }
}


*** 2) Your JerseyConfig class to register resources *** 

package com.ilearn.jersey.config;

import com.ilearn.jersey.resource.AutoCompletionResource;
import com.ilearn.jersey.resource.HealthCheckResource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.context.annotation.Configuration;

import javax.ws.rs.ApplicationPath;


@Configuration
@ApplicationPath("/i-learn")
public class JerseyConfig extends ResourceConfig {

    private static final Logger logger = LogManager.getLogger(JerseyConfig.class);

    public JerseyConfig() {
        logger.info("Registering endpoints classes...");
        this.registerEndpoints();
        logger.info("Registered endpoints classes");
    }

    private void registerEndpoints() {
        register(HealthCheckResource.class);
        register(AutoCompletionResource.class);
    }
}


**3) pom.xml:** 

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
            <version>2.33</version>
        </dependency>

    </dependencies>

**4) Run application using url:** 
   http://localhost:8080/i-learn/health-check 

   Output:
   {
    "i-learn": "Running"
   }
Owing answered 30/9, 2021 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.