0
這裏是澤西島應用程序的代碼spring.I使用resttemplate檢查休息終點。 我想使用Appconfig類來配置resttemplate。我的意思是,當創建resttemplate實例時,它應該從appconfig restTemplate()函數創建。有可能這樣做嗎?如何使用澤西應用程序中的配置
//JerseyApplication.java
public class JerseyApplication extends ResourceConfig
{
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(JerseyApplication.class);
/**
* Initialized the jersey application.
*/
public JerseyApplication()
{
register(JerseyFeature.class);
register(JsonFeature.class);
register(BeanValidationFeature.class);
register(new RequestResponseLoggingFilter(LOG));
}
}
//TestResource.java
@Component("apiTestResource")
@PropertySource("classpath:default.properties")
@Singleton
public class TestResource{
@javax.ws.rs.core.Context
private javax.ws.rs.core.UriInfo uriInfo;
@Autowired
private RestTemplate restTemplate;
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(TestResource.class);
public Response post() {
Response response = null;
UriComponentsBuilder uriComponents = UriComponentsBuilder.fromHttpUrl("http://test.com")
HttpEntity<String> requestPayload = new HttpEntity<String>("test string");
ResponseEntity<String> responseEntity = util.postSyncRequest(restTemplate,uriComponents.build().encode().toUri(),
requestPayload);
response = Response.ok().entity(responseEntity.getBody().toString()).build();
return response;
}
}
//Util.java
public class Util {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(Util.class);
public ResponseEntity<String> postSyncRequest(RestTemplate restTemplate ,URI uri, HttpEntity<String> requestPayload){
ResponseEntity<String> responseEntity = null;
try{
responseEntity = restTemplate.postForEntity(uri, requestPayload,String.class);
}catch(Exception ex){
LOG.error("Error in post call " + ex.getMessage());
}
return responseEntity;
}
}
//AppConfig.java
@Configuration
public class AppConfig {
@Bean
RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
return new RestTemplate(requestFactory);
}
}
//applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- include Service SDK libraries -->
<import resource="classpath:/META-INF/libraries-spring.xml" />
<!-- include Service SDK API-Console helpers -->
<import resource="classpath*:/META-INF/api-console-spring.xml" />
<!-- import placeholder values from property files and environment, see
default.properties -->
<context:property-placeholder
location="classpath:/default.properties,classpath*:test.properties" />
<!-- take annotation-based configuration into account, when instantiating
beans -->
<context:annotation-config />
<bean id="RestTemplate" class="org.springframework.web.client.RestTemplate">
</bean>
</beans>
更新: 想什麼@jobin建議我收到以下錯誤
[ERROR] [o.s.web.context.ContextLoader] [] Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'apiCurrencyS4hanaResource': Unsatisfied dependency expressed through field 'restTemplate': No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency [org.springframework.web.client.RestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.client.RestTemplate] found for dependency [org.springframework.web.client.RestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
您已經在執行此操作,請刪除您在applicationContext.xml中定義的'RestTemplate' – kuhajeyan