本文整理汇总了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]);
}
示例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);
}
}
示例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);
}
}
示例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);
}
示例5: GsonHttpMessageConverter
public GsonHttpMessageConverter() {
super(new MediaType[]{MediaType.APPLICATION_JSON, new MediaType("application", "*+json")});
}
示例6: createServer
public static TuPactRecordingServer createServer(RestTemplate restTemplate, File pactFile) {
return new TuPactRecordingServer(restTemplate, pactFile, new GsonStringConverter(new GsonBuilder().create()), MediaType.APPLICATION_JSON);
}