Springboot RestCall具有注入依赖性

时间:2018-02-06 14:00:13

标签: rest spring-boot

我有一个带有弹簧启动的以下结构的项目,当我运行项目并尝试访问URL http:// localhost:8080 / sample / details它不起作用,但当我删除所有注释除了@SpringBootApplicaiton,那么同样适用于url。任何人都可以提示,我在做错误吗?

compute

Main SpringBootClass类:

com.sample.controller
    --Main class (with annotated as @SpringBootApplication)
    --ControllerClass (which takes the @RequestMapping values)
com.sample.vo
    --POJO Class
com.sample.service
    --Interface class
com.sample.serviceImpl
    --Service implementation class

控制器类:

@SpringBootApplication
@ComponentScan({
                "com.sample.service",
                "com.sample.serviceImpl"
               })
public class ServletContainer {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ServletContainer.class)
                .properties("spring.config.name:application",                       "spring.config.location:classpath:/propertyFileLication/").build().run(args);
    }
}

POJO班级:

@RestController
@RequestMapping("/sample")
public class SampleController {

    @Autowired
    private SampleVO SampleVO;

    @Autowired
    private SampleService SampleService;

    @RequestMapping(value="/details",produces = MediaType.APPLICATION_JSON)
    public List<SampleVO> prodDetails(){
        SampleVO SampleVO = new SampleVO();
        SampleService SampleService = new SampleServiceImpl();
        List<SampleVO> list = SampleService.fetchProductDetails(SampleVO);
        return list;
    }
}

服务接口类:

@Service
public class sampleVO {
    //something
}

服务实施类:

@Service
public interface SampleService {
    public List<SampleVO> fetchProductDetails(SampleVO SampleVO);
}

1 个答案:

答案 0 :(得分:0)

稍微改变项目结构后,它就变得有效了。

我如何改变

com.sample.initializer
     --Main class (with annotated as @SpringBootApplication)
com.sample.controller
    --ControllerClass (which takes the @RequestMapping values)
com.sample.vo
    --POJO Class
com.sample.service
    --Interface class
com.sample.serviceImpl
    --Service implementation class


@ComponentScan({
                "com.sample.controller",
                "com.sample.vo",
                "com.sample.service",
                "com.sample.serviceImpl"
               })

有人可以告诉我,我们是否需要在单独的包中处理控制器和Spring-boot初始化器类。