Here is code of Jersey application with spring.I am using resttemplate to check a rest end point. I want to configure resttemplate using Appconfig class.what I mean is that when resttemplate instance is created, it should be created from appconfig restTemplate() function. Is it possible to do that?
//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>
Update : After trying what @jobin has recommended I am getting following error
[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)}
Yes it is possible, what you did is correct but SimpleClientHttpRequestFactory
is not needed, you can simply create an instance of RestTemplate
as shown below, it will create a singleton
bean which you can autowire
to other spring components
import org.springframework.web.client.RestTemplate;
@Configuration
public class AppConfig {
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.