0

I am trying to get values from the function but failing. I tried adding componentscan but I failed.

Main method

package org.vik.springstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@SpringBootApplication
public class MyMain {
     public static void main(String[] args) {
        SpringApplication.run(MyMain.class, args);
    }
}

User Class

package org.vik.data;

public class UserData {
    private Integer Id;
    private String name;
    private String address;

    public UserData(Integer Id,String name,String address){
        this.Id = Id;
        this.name = name;
        this.address = address;
    }
    public Integer getId() {
        return Id;
    }
    public void setId(Integer id) {
        Id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

User Service

package org.vik.data;
import java.util.Arrays;
import java.util.List;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
@Component
public class UserService {

    private List<UserData> userlist = Arrays.asList(
        new UserData(1,"Vik","sdfdsf"),
        new UserData(1,"Abani","sdfdsf"),
        new UserData(1,"Abrar","sdfdsf")
    );

    public List<UserData> getAlluser(){
        return userlist;
    }
 }

User Controller

package org.vik.data;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userservice;
    @RequestMapping("/hello")
    public List<UserData> Hello(){
        return userservice.getAlluser();
    }
    @RequestMapping("/vik")
    public String hi(){
        return "hi";
    }
}

*************************** APPLICATION FAILED TO START


Description:

Field userservice in org.vik.springstarter.controller.HelloController required a bean of type 'org.vik.data.UserService' that could not be found.

Action:

Consider defining a bean of type 'org.vik.data.UserService' in your configuration.

ADyson
  • 57,178
  • 14
  • 51
  • 63

1 Answers1

0

Your Application.class is in a different package structure:

package org.vik.springstarter;

Just move your Application.class to the same structure of your Service:

org.vik