当前位置: 首页>>代码示例>>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;未经允许,请勿转载。