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


Java MediaType.APPLICATION_JSON屬性代碼示例

本文整理匯總了Java中org.springframework.http.MediaType.APPLICATION_JSON屬性的典型用法代碼示例。如果您正苦於以下問題:Java MediaType.APPLICATION_JSON屬性的具體用法?Java MediaType.APPLICATION_JSON怎麽用?Java MediaType.APPLICATION_JSON使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在org.springframework.http.MediaType的用法示例。


在下文中一共展示了MediaType.APPLICATION_JSON屬性的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: selectHeaderContentType

/**
 * Select the Content-Type header's value from the given array:
 *     if JSON exists in the given array, use it;
 *     otherwise use the first one of the array.
 *
 * @param contentTypes The Content-Type array to select from
 * @return MediaType The Content-Type header to use. If the given array is empty, JSON will be used.
 */
public MediaType selectHeaderContentType(String[] contentTypes) {
    if (contentTypes.length == 0) {
        return MediaType.APPLICATION_JSON;
    }
    for (String contentType : contentTypes) {
        MediaType mediaType = MediaType.parseMediaType(contentType);
        if (isJsonMime(mediaType)) {
            return mediaType;
        }
    }
    return MediaType.parseMediaType(contentTypes[0]);
}
 
開發者ID:jopache,項目名稱:Settings,代碼行數:20,代碼來源:ApiClient.java

示例2: render

/**
 * Render model and view.
 *
 * @param model the model
 * @param response the response
 */
public static void render(final Object model, final HttpServletResponse response) {
    try {
        final MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        jsonConverter.setPrettyPrint(true);
        final MediaType jsonMimeType = MediaType.APPLICATION_JSON;
        jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:16,代碼來源:JsonViewUtils.java

示例3: render

/**
 * Render model and view.
 *
 * @param model the model
 * @param response the response
 */
public static void render(final Object model, final HttpServletResponse response) {
    try {
        final MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        jsonConverter.setPrettyPrint(true);
        final MediaType jsonMimeType = MediaType.APPLICATION_JSON;
        jsonConverter.write(model, jsonMimeType, new ServletServerHttpResponse(response));
    } catch (final Exception e) {
        throw Throwables.propagate(e);
    }
}
 
開發者ID:mrluo735,項目名稱:cas-5.1.0,代碼行數:16,代碼來源:JsonUtils.java

示例4: test_1

public void test_1() throws Exception {

		FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

		Assert.assertNotNull(converter.getFastJsonConfig());
		converter.setFastJsonConfig(new FastJsonConfig());

		converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
		converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
		converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
		converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);

		HttpInputMessage input = new HttpInputMessage() {

			public HttpHeaders getHeaders() {
				// TODO Auto-generated method stub
				return null;
			}

			public InputStream getBody() throws IOException {
				return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset
						.forName("UTF-8")));
			}

		};
		VO vo = (VO) converter.read(VO.class, VO.class, input);
		Assert.assertEquals(123, vo.getId());

		final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		HttpOutputMessage out = new HttpOutputMessage() {

			public HttpHeaders getHeaders() {
				return new HttpHeaders();
			}

			public OutputStream getBody() throws IOException {
				return byteOut;
			}
		};
		converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);

		byte[] bytes = byteOut.toByteArray();
		Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));

		converter.setSupportedMediaTypes(Collections
				.singletonList(MediaType.APPLICATION_JSON));

		converter.write(vo, VO.class, null, out);

		converter.write(vo, VO.class, MediaType.ALL, out);

		HttpOutputMessage out2 = new HttpOutputMessage() {

			public HttpHeaders getHeaders() {

				return new HttpHeaders() {

					private static final long serialVersionUID = 1L;

					@Override
					public MediaType getContentType() {

						return MediaType.APPLICATION_JSON;
					}

					@Override
					public long getContentLength() {

						return 1;
					}
				};
			}

			public OutputStream getBody() throws IOException {
				return byteOut;
			}
		};

		converter.write(vo, VO.class, MediaType.ALL, out2);

	}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:81,代碼來源:FastJsonHttpMessageConverterTest.java

示例5: GsonHttpMessageConverter

public GsonHttpMessageConverter() {
  super(new MediaType[]{MediaType.APPLICATION_JSON, new MediaType("application", "*+json")});
}
 
開發者ID:Huawei,項目名稱:Server_Management_Common_eSightApi,代碼行數:3,代碼來源:GsonHttpMessageConverter.java

示例6: createServer

public static TuPactRecordingServer createServer(RestTemplate restTemplate, File pactFile) {
    return new TuPactRecordingServer(restTemplate, pactFile, new GsonStringConverter(new GsonBuilder().create()), MediaType.APPLICATION_JSON);
}
 
開發者ID:tyro,項目名稱:pact-spring-mvc,代碼行數:3,代碼來源:TuPactRecordingServer.java


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