本文整理汇总了Java中org.springframework.web.client.RestTemplate.setMessageConverters方法的典型用法代码示例。如果您正苦于以下问题:Java RestTemplate.setMessageConverters方法的具体用法?Java RestTemplate.setMessageConverters怎么用?Java RestTemplate.setMessageConverters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.client.RestTemplate
的用法示例。
在下文中一共展示了RestTemplate.setMessageConverters方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendPostForUpload
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
*
* /** sendPostCommand
*
* @param url
* @param parameters
* @return
* @throws ClientProtocolException
*/
public Map<String, Object> sendPostForUpload(String url, Map<String, Object> parameters) {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
RestTemplate restTemplate = new RestTemplate(requestFactory);
List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
mc.add(new MappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(mc);
MultiValueMap<String, Object> postParams = new LinkedMultiValueMap<String, Object>();
postParams.setAll(parameters);
Map<String, Object> response = new HashMap<String, Object>();
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data");
headers.set("Accept", "application/json");
headers.add("Cookie", "JSESSIONID=" + localContext.getCookieStore().getCookies().get(0).getValue());
HttpEntity<Object> request = new HttpEntity<Object>(postParams, headers);
ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
String body = result.getBody().toString();
MediaType contentType = result.getHeaders().getContentType();
HttpStatus statusCode = result.getStatusCode();
response.put(CONTENT_TYPE, contentType);
response.put(STATUS_CODE, statusCode);
response.put(BODY, body);
return response;
}
示例2: createRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Bean
public RestTemplate createRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
jsonMessageConverter.setObjectMapper(objectMapper);
messageConverters.add(jsonMessageConverter);
restTemplate.setMessageConverters(messageConverters);
return restTemplate;
}
开发者ID:berndruecker,项目名称:camunda-spring-boot-amqp-microservice-cloud-example,代码行数:11,代码来源:RestConfiguration.java
示例3: createRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public RestTemplate createRestTemplate(HttpProxyConfiguration httpProxyConfiguration, boolean
trustSelfSignedCerts) {
RestTemplate restTemplate = new LoggingRestTemplate();
restTemplate.setRequestFactory(createRequestFactory(httpProxyConfiguration, trustSelfSignedCerts));
restTemplate.setErrorHandler(new CloudControllerResponseErrorHandler());
restTemplate.setMessageConverters(getHttpMessageConverters());
return restTemplate;
}
示例4: configureRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Configure a {@link RestTemplate} for communication with a CredHub server.
* @param restTemplate an existing {@link RestTemplate} to configure
* @param baseUri the base URI for the CredHub server
* @param clientHttpRequestFactory the {@link ClientHttpRequestFactory} to use when
* creating new connections
*/
public static void configureRestTemplate(RestTemplate restTemplate, String baseUri,
ClientHttpRequestFactory clientHttpRequestFactory) {
restTemplate.setRequestFactory(clientHttpRequestFactory);
restTemplate.setUriTemplateHandler(createUriTemplateHandler(baseUri));
restTemplate.setMessageConverters(createMessageConverters());
restTemplate.setInterceptors(createInterceptors());
}
示例5: createRestTemplateWithJsonSupport
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public static RestTemplate createRestTemplateWithJsonSupport(final String proxyHost, final int proxyPort) {
RestTemplate rt = createRestTemplate(proxyHost, proxyPort);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
rt.setMessageConverters(messageConverters);
return rt;
}
示例6: init
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@PostConstruct
public void init() {
restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory rf = (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory();
rf.setReadTimeout(portalConfig.readTimeout());
rf.setConnectTimeout(portalConfig.connectTimeout());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(
Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
restTemplate.setMessageConverters(Arrays.asList(converter, new FormHttpMessageConverter()));
}
示例7: createClientRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
private RestTemplate createClientRestTemplate(String clientToken) {
RestTemplate restTemplate = new ClientAuthorizedTwitterTemplate(clientToken).getRestTemplate();
restTemplate.setMessageConverters(getMessageConverters());
configureRestTemplate(restTemplate);
return restTemplate;
}
示例8: run
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public void run() throws Exception {
final File file = new File(inputFile);
if (!file.exists() || !file.isFile()) {
System.err.format("unable to access file %s\n", inputFile);
System.exit(2);
}
// Open file
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// Initialize RestTemplate
RestTemplate restTemplate = new RestTemplate();
// Read each line
String line = null;
while ((line = br.readLine()) != null) {
String[] fields = line.split(",");
// Maybe strip quotes
for (int i=0; i<fields.length; i++) {
final String field = fields[i];
if (field.matches("^\".*\"$")) {
fields[i] = field.substring(1, field.length()-1);
}
}
final HttpHeaders requestHeaders = new HttpHeaders();
final Object transformRequest;
if (isSequential == true) {
requestHeaders.add("Sequence", "true");
transformRequest = new TransformedArray.BatchedRequest(fields);
} else {
transformRequest = new TransformedArray.Request(fields);
}
if (textAsJson) {
// Accept JSON
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
// Temp fix
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters.add(new ExtendedMappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
}
final HttpEntity<Object> httpEntity =
new HttpEntity<Object>(transformRequest, requestHeaders);
final TransformedArray.Response arrayResponse = restTemplate.postForObject(
transformedArrayEndpoint,
httpEntity,
TransformedArray.Response.class);
Class clazz;
Object request;
if (inferenceType == InferenceType.Single || inferenceType == InferenceType.Multi) {
clazz = (inferenceType == InferenceType.Single) ?
Inference.Response.Classify.class : Inference.Response.MultiClassify.class;
request = new Inference.Request(arrayResponse.getNdArray());
} else {
clazz = Knn.Response.class;
request = new Knn.Request(knnN, arrayResponse.getNdArray());
}
final Object response = restTemplate.postForObject(
inferenceEndpoint,
request,
clazz);
System.out.format("Inference response: %s\n", response.toString());
}
br.close();
}
示例9: run
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public void run() throws Exception {
final File file = new File(inputFile);
if (!file.exists() || !file.isFile()) {
System.err.format("unable to access file %s\n", inputFile);
System.exit(2);
}
// Open file
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// Initialize RestTemplate
RestTemplate restTemplate = new RestTemplate();
if (textAsJson) {
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
converters.add(new ExtendedMappingJackson2HttpMessageConverter());
restTemplate.setMessageConverters(converters);
}
// Read each line
String line = null;
while ((line = br.readLine()) != null) {
// Check if label indicator is up front
String label = null;
if (line.matches("^\\d:\\s.*")) {
label = line.substring(0, 1);
}
// Just in case
line = StringUtils.removePattern(line, "^\\d:\\s");
String[] fields = line.split(",");
// Maybe strip quotes
for (int i = 0; i < fields.length; i++) {
final String field = fields[i];
if (field.matches("^\".*\"$")) {
fields[i] = field.substring(1, field.length() - 1);
}
}
int[] shape = (isSequential) ?
new int[] { 1, 1, fields.length} :
new int[] { 1, fields.length};
INDArray array = Nd4j.create(shape);
for (int i=0; i<fields.length; i++) {
// TODO: catch NumberFormatException
Double d = Double.parseDouble(fields[i]);
int[] idx = (isSequential) ?
new int[]{0, 0, i} :
new int[]{0, i};
array.putScalar(idx, d);
}
Inference.Request request = new Inference.Request(Nd4jBase64.base64String(array));
final Object response = restTemplate.postForObject(
inferenceEndpoint,
request,
Inference.Response.Classify.class);
System.out.format("Inference response: %s\n", response.toString());
if (label != null) {
System.out.format(" Label expected: %s\n", label);
}
}
br.close();
}
示例10: testItemSetCreated
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetCreated() {
String appId = "someAppId";
AppDTO app =
restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
ClusterDTO cluster = restTemplate.getForObject(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
ClusterDTO.class);
NamespaceDTO namespace =
restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
+ "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
Assert.assertEquals("someAppId", app.getAppId());
Assert.assertEquals("default", cluster.getName());
Assert.assertEquals("application", namespace.getNamespaceName());
ItemChangeSets itemSet = new ItemChangeSets();
itemSet.setDataChangeLastModifiedBy("created");
RestTemplate createdTemplate = new TestRestTemplate("created", "");
createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
int createdSize = 3;
for (int i = 0; i < createdSize; i++) {
ItemDTO item = new ItemDTO();
item.setNamespaceId(namespace.getId());
item.setKey("key_" + i);
item.setValue("created_value_" + i);
itemSet.addCreateItem(item);
}
ResponseEntity<Void> response =
createdTemplate.postForEntity(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
+ cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/itemset",
itemSet, Void.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
List<Item> items = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
Assert.assertEquals(createdSize, items.size());
Item item0 = items.get(0);
Assert.assertEquals("key_0", item0.getKey());
Assert.assertEquals("created_value_0", item0.getValue());
Assert.assertEquals("created", item0.getDataChangeCreatedBy());
Assert.assertNotNull(item0.getDataChangeCreatedTime());
}
示例11: testItemSetUpdated
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetUpdated() {
String appId = "someAppId";
AppDTO app =
restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
ClusterDTO cluster = restTemplate.getForObject(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
ClusterDTO.class);
NamespaceDTO namespace =
restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
+ "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
Assert.assertEquals("someAppId", app.getAppId());
Assert.assertEquals("default", cluster.getName());
Assert.assertEquals("application", namespace.getNamespaceName());
ItemChangeSets createChangeSet = new ItemChangeSets();
createChangeSet.setDataChangeLastModifiedBy("created");
RestTemplate createdRestTemplate = new TestRestTemplate("created", "");
createdRestTemplate.setMessageConverters(restTemplate.getMessageConverters());
int createdSize = 3;
for (int i = 0; i < createdSize; i++) {
ItemDTO item = new ItemDTO();
item.setNamespaceId(namespace.getId());
item.setKey("key_" + i);
item.setValue("created_value_" + i);
createChangeSet.addCreateItem(item);
}
ResponseEntity<Void> response = createdRestTemplate.postForEntity(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
+ "/namespaces/" + namespace.getNamespaceName() + "/itemset",
createChangeSet, Void.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
ItemDTO[] items =
createdRestTemplate.getForObject(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
+ cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items",
ItemDTO[].class);
ItemChangeSets updateChangeSet = new ItemChangeSets();
updateChangeSet.setDataChangeLastModifiedBy("updated");
RestTemplate updatedRestTemplate = new TestRestTemplate("updated", "");
updatedRestTemplate.setMessageConverters(restTemplate.getMessageConverters());
int updatedSize = 2;
for (int i = 0; i < updatedSize; i++) {
items[i].setValue("updated_value_" + i);
updateChangeSet.addUpdateItem(items[i]);
}
response = updatedRestTemplate.postForEntity(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
+ "/namespaces/" + namespace.getNamespaceName() + "/itemset",
updateChangeSet, Void.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
Assert.assertEquals(createdSize, savedItems.size());
Item item0 = savedItems.get(0);
Assert.assertEquals("key_0", item0.getKey());
Assert.assertEquals("updated_value_0", item0.getValue());
Assert.assertEquals("created", item0.getDataChangeCreatedBy());
Assert.assertEquals("updated", item0.getDataChangeLastModifiedBy());
Assert.assertNotNull(item0.getDataChangeCreatedTime());
Assert.assertNotNull(item0.getDataChangeLastModifiedTime());
}
示例12: testItemSetDeleted
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
@Test
@Sql(scripts = "/controller/test-itemset.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testItemSetDeleted() {
String appId = "someAppId";
AppDTO app =
restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
ClusterDTO cluster = restTemplate.getForObject(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default",
ClusterDTO.class);
NamespaceDTO namespace =
restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId()
+ "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
Assert.assertEquals("someAppId", app.getAppId());
Assert.assertEquals("default", cluster.getName());
Assert.assertEquals("application", namespace.getNamespaceName());
ItemChangeSets createChangeSet = new ItemChangeSets();
createChangeSet.setDataChangeLastModifiedBy("created");
RestTemplate createdTemplate = new TestRestTemplate("created", "");
createdTemplate.setMessageConverters(restTemplate.getMessageConverters());
int createdSize = 3;
for (int i = 0; i < createdSize; i++) {
ItemDTO item = new ItemDTO();
item.setNamespaceId(namespace.getId());
item.setKey("key_" + i);
item.setValue("created_value_" + i);
createChangeSet.addCreateItem(item);
}
ResponseEntity<Void> response = createdTemplate.postForEntity(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
+ "/namespaces/" + namespace.getNamespaceName() + "/itemset",
createChangeSet, Void.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
ItemDTO[] items =
restTemplate.getForObject(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/"
+ cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items",
ItemDTO[].class);
ItemChangeSets deleteChangeSet = new ItemChangeSets();
deleteChangeSet.setDataChangeLastModifiedBy("deleted");
RestTemplate deletedTemplate = new TestRestTemplate("deleted", "");
deletedTemplate.setMessageConverters(restTemplate.getMessageConverters());
int deletedSize = 1;
for (int i = 0; i < deletedSize; i++) {
items[i].setValue("deleted_value_" + i);
deleteChangeSet.addDeleteItem(items[i]);
}
response = deletedTemplate.postForEntity(
"http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName()
+ "/namespaces/" + namespace.getNamespaceName() + "/itemset",
deleteChangeSet, Void.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
List<Item> savedItems = itemRepository.findByNamespaceIdOrderByLineNumAsc(namespace.getId());
Assert.assertEquals(createdSize - deletedSize, savedItems.size());
Item item0 = savedItems.get(0);
Assert.assertEquals("key_1", item0.getKey());
Assert.assertEquals("created_value_1", item0.getValue());
Assert.assertEquals("created", item0.getDataChangeCreatedBy());
Assert.assertNotNull(item0.getDataChangeCreatedTime());
}