本文整理汇总了Java中org.springframework.web.client.RestTemplate.getMessageConverters方法的典型用法代码示例。如果您正苦于以下问题:Java RestTemplate.getMessageConverters方法的具体用法?Java RestTemplate.getMessageConverters怎么用?Java RestTemplate.getMessageConverters使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.web.client.RestTemplate
的用法示例。
在下文中一共展示了RestTemplate.getMessageConverters方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: validateRestTemplate
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
public RestTemplate validateRestTemplate(RestTemplate restTemplate) {
boolean containsMappingJackson2HttpMessageConverter = false;
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
containsMappingJackson2HttpMessageConverter = true;
}
}
if (!containsMappingJackson2HttpMessageConverter) {
throw new IllegalArgumentException(
"The RestTemplate does not contain a required " + "MappingJackson2HttpMessageConverter.");
}
return restTemplate;
}
示例3: getGsonConverter
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Get a registered GsonHttpMessageConverter from RestTemplate
* @param restTemplate RestTemplate
* @return Optional GsonHttpMessageConverter, empty if not registered
*/
private static Optional<GsonHttpMessageConverter> getGsonConverter(RestTemplate restTemplate) {
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
if (GsonHttpMessageConverter.class.isAssignableFrom(converter.getClass())) {
return Optional.of((GsonHttpMessageConverter) converter);
}
}
return Optional.empty();
}
示例4: getJacksonConverter
import org.springframework.web.client.RestTemplate; //导入方法依赖的package包/类
/**
* Get a registered AbstractJackson2HttpMessageConverter from RestTemplate
* @param restTemplate RestTemplate
* @return Optional AbstractJackson2HttpMessageConverter, empty if not registered
*/
private static Optional<AbstractJackson2HttpMessageConverter> getJacksonConverter(RestTemplate restTemplate) {
for (HttpMessageConverter<?> converter : restTemplate.getMessageConverters()) {
if (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converter.getClass())) {
return Optional.of((AbstractJackson2HttpMessageConverter) converter);
}
}
return Optional.empty();
}
示例5: 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();
}
示例6: 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();
}