无法自动装配Spring Rest Template

时间:2016-10-17 08:44:07

标签: java spring rest spring-mvc spring-rest

以下是我的其余模板配置:

<beans:bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <beans:constructor-arg>
        <beans:bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
            <beans:property name="readTimeout" value="600000" />
            <beans:property name="connectTimeout" value="600000" />
        </beans:bean>
    </beans:constructor-arg>
    <beans:property name="messageConverters">
        <beans:list>
            <beans:bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
            </beans:bean>
            <beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
                <beans:property name="objectMapper">
                    <beans:ref bean="jacksonObjectMapper" />
                </beans:property>
                <beans:property name="supportedMediaTypes">
                    <beans:list>
                        <beans:value>text/plain</beans:value>
                        <beans:value>application/json</beans:value>
                    </beans:list>
                </beans:property>
            </beans:bean>


        </beans:list>
    </beans:property>
</beans:bean>

当我尝试自动装配并访问类中的模板时,我得到了一个N​​PE:

@Service
public class GitService {

    @Autowired
    RestTemplate restTemplate;

    public void getTot(String url) {

        List<ResponseObject> eg = (List<ResponseObject>) restTemplate.getForObject(url,ResponseObject.class);
    }
}

例外:

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/github] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.assignment.github.service.GitService.getTot(GitService.java:25)

有人可以告诉我这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

请添加这些标记

<context:annotation-config/>

如果你还没有

<context:component-scan base-package="<Your_base_package"/>

进入相同的配置文件。