當前位置: 首頁>>代碼示例>>Java>>正文


Java RestTemplate.getMessageConverters方法代碼示例

本文整理匯總了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;

}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:38,代碼來源:RestUtils.java

示例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;
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-skipper,代碼行數:16,代碼來源:SkipperClientConfiguration.java

示例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();
}
 
開發者ID:holon-platform,項目名稱:holon-json,代碼行數:14,代碼來源:SpringGsonConfiguration.java

示例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();
}
 
開發者ID:holon-platform,項目名稱:holon-json,代碼行數:14,代碼來源:SpringJacksonConfiguration.java

示例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();
}
 
開發者ID:SkymindIO,項目名稱:SKIL_Examples,代碼行數:80,代碼來源:ModelServerInferenceExample.java

示例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();
}
 
開發者ID:SkymindIO,項目名稱:SKIL_Examples,代碼行數:72,代碼來源:ModelServerDirectInferenceExample.java


注:本文中的org.springframework.web.client.RestTemplate.getMessageConverters方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。