本文整理匯總了Java中org.springframework.http.converter.StringHttpMessageConverter類的典型用法代碼示例。如果您正苦於以下問題:Java StringHttpMessageConverter類的具體用法?Java StringHttpMessageConverter怎麽用?Java StringHttpMessageConverter使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
StringHttpMessageConverter類屬於org.springframework.http.converter包,在下文中一共展示了StringHttpMessageConverter類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addDefaultHttpMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
/**
* Adds a set of default HttpMessageConverter instances to the given list.
* Subclasses can call this method from {@link #configureMessageConverters(List)}.
* @param messageConverters the list to add the default message converters to
*/
protected final void addDefaultHttpMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
StringHttpMessageConverter stringConverter = new StringHttpMessageConverter();
stringConverter.setWriteAcceptCharset(false);
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(stringConverter);
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<Source>());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
if (romePresent) {
messageConverters.add(new AtomFeedHttpMessageConverter());
messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jaxb2Present) {
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {
messageConverters.add(new MappingJackson2HttpMessageConverter());
}
else if (jacksonPresent) {
messageConverters.add(new MappingJacksonHttpMessageConverter());
}
}
示例2: EmailServiceImpl
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
EmailServiceImpl(Environment env,
TextService text) {
this.env = requireNonNull(env);
this.text = requireNonNull(text);
this.mailgun = new RestTemplate();
this.mailgun.setMessageConverters(asList(
new FormHttpMessageConverter(),
new StringHttpMessageConverter(),
new MappingJackson2HttpMessageConverter()
));
this.mailgun.getInterceptors().add(new BasicAuthorizationInterceptor(
"api", env.getProperty("mailgun.apiKey")
));
}
示例3: getTemplate
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
public static RestTemplate getTemplate(ClientHttpRequestInterceptor interceptor) {
RestTemplate restTemplate = new RestTemplate();
List<ClientHttpRequestInterceptor> ris = new ArrayList<>();
ris.add(interceptor);
restTemplate.setInterceptors(ris);
SimpleClientHttpRequestFactory httpFactory = new SimpleClientHttpRequestFactory();
httpFactory.setOutputStreaming(false);
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(httpFactory));
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
示例4: RestTemplate
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
/**
* Create a new instance of the {@link RestTemplate} using default settings.
* Default {@link HttpMessageConverter}s are initialized.
*/
@SuppressWarnings("deprecation")
public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
this.messageConverters.add(new ResourceHttpMessageConverter());
this.messageConverters.add(new SourceHttpMessageConverter<Source>());
this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());
if (romePresent) {
this.messageConverters.add(new AtomFeedHttpMessageConverter());
this.messageConverters.add(new RssChannelHttpMessageConverter());
}
if (jaxb2Present) {
this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
if (jackson2Present) {
this.messageConverters.add(new MappingJackson2HttpMessageConverter());
}
else if (jacksonPresent) {
this.messageConverters.add(new org.springframework.http.converter.json.MappingJacksonHttpMessageConverter());
}
}
示例5: AbstractZg2proRestTemplate
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
protected AbstractZg2proRestTemplate(SimpleModule sm) {
super();
//converters
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
ObjectMapper jsonMapper = camelToKebabObjectMapper(sm);
messageConverters.add(new MappingJackson2HttpMessageConverter(jsonMapper));
this.setMessageConverters(messageConverters);
//errors handling
setErrorHandler(new RestTemplateErrorHandler());
/*
//interceptors
LoggingRequestInterceptor lri = new LoggingRequestInterceptor();
this.setInterceptors(new ArrayList<>());
this.getInterceptors().add(lri);
this.setRequestFactory(LoggingRequestFactoryFactory.build(lri));
*/
}
示例6: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
final MappingJackson2HttpMessageConverter jsonMessageConverter = jacksonMessageConverter();
final StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
stringHttpMessageConverter.setWriteAcceptCharset(false);
messageConverters.add(new CSVMessageConverter());
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(stringHttpMessageConverter);
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new SourceHttpMessageConverter<>());
messageConverters.add(new AllEncompassingFormHttpMessageConverter());
messageConverters.add(jsonMessageConverter);
messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
}
示例7: setUp
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Before
public void setUp() throws Exception {
List<HttpMessageConverter<?>> converters = Arrays.asList(
new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter());
this.handler = new ResponseBodyEmitterReturnValueHandler(converters);
this.mavContainer = new ModelAndViewContainer();
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.webRequest = new ServletWebRequest(this.request, this.response);
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
this.request.setAsyncSupported(true);
}
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:18,代碼來源:ResponseBodyEmitterReturnValueHandlerTests.java
示例8: resolveArgumentClassString
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Test
public void resolveArgumentClassString() throws Exception {
String content = "foobarbaz";
this.servletRequest.setContent(content.getBytes("UTF-8"));
this.servletRequest.setContentType("application/json");
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new StringHttpMessageConverter());
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
String result = (String) processor.resolveArgument(
paramString, mavContainer, webRequest, binderFactory);
assertNotNull(result);
assertEquals("foobarbaz", result);
}
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:RequestResponseBodyMethodProcessorTests.java
示例9: responseBodyNoAcceptableMediaType
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Test
public void responseBodyNoAcceptableMediaType() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyProducesController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/pdf, application/msword");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(406, response.getStatus());
}
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:22,代碼來源:ServletAnnotationControllerHandlerMethodTests.java
示例10: overlappingMessageConvertersRequestBody
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Test
public void overlappingMessageConvertersRequestBody() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new StringHttpMessageConverter());
messageConverters
.add(new SimpleMessageConverter(new MediaType("application","json"), MediaType.ALL));
adapterDef.getPropertyValues().add("messageConverters", messageConverters);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestResponseBodyController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
request.setContent("Hello World".getBytes("UTF-8"));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/json, text/javascript, */*");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Invalid content-type", "application/json", response.getHeader("Content-Type"));
}
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:ServletAnnotationControllerHandlerMethodTests.java
示例11: verifyNoMarkupError
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
/**
* Verifies that the HTML is well formed.
*
* @param markup The HTML markup to be tested.
*/
public static void verifyNoMarkupError(String markup) {
// List<MediaType> supportedMediaTypes = new ArrayList<>();
List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
supportedMediaTypes.add(new MediaType("text", "html", Charset.forName("UTF-8")));
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
// List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(stringHttpMessageConverter);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(messageConverters);
String result = restTemplate.postForObject(URL, markup, String.class);
System.out.println("result: " + result);
assertTrue("The document is not valid HTML5: " + markup, result.contains("The document is valid HTML5"));
}
示例12: AuthenticatedRequest
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
/**
* Instantiates a new Authenticated request.
*
* @param requestReason the request reason
* @param context the context
* @param loadingMessage the loading message
* @param connectionInformation the connection information
* @param userName the user name
* @param password the password
*/
public AuthenticatedRequest(RequestReason requestReason, OnHttpRequestFinishedCallback context,
String loadingMessage, ConnectionInformation connectionInformation,
String userName, String password) {
super(requestReason, context, loadingMessage, connectionInformation);
this.userName = userName;
this.password = password;
// Set the username and password for creating a Basic Auth request
authHeader = new HttpBasicAuthentication(userName, password);
requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
requestEntity = new HttpEntity<Object>(requestHeaders);
// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
}
示例13: registerHttpMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
private String registerHttpMessageConverters(final String id) {
return registry.register(id, HttpMessageConverters.class, () -> {
final List<Object> list = list();
LOG.debug("Client [{}]: Registering StringHttpMessageConverter", id);
list.add(genericBeanDefinition(StringHttpMessageConverter.class)
.addPropertyValue("writeAcceptCharset", false)
.getBeanDefinition());
final String objectMapperId = findObjectMapper(id);
LOG.debug("Client [{}]: Registering MappingJackson2HttpMessageConverter referencing [{}]", id, objectMapperId);
list.add(genericBeanDefinition(MappingJackson2HttpMessageConverter.class)
.addConstructorArgReference(objectMapperId)
.getBeanDefinition());
LOG.debug("Client [{}]: Registering StreamConverter referencing [{}]", id, objectMapperId);
list.add(genericBeanDefinition(Streams.class)
.setFactoryMethod("streamConverter")
.addConstructorArgReference(objectMapperId)
.getBeanDefinition());
return BeanDefinitionBuilder.genericBeanDefinition(ClientHttpMessageConverters.class)
.addConstructorArgValue(list);
});
}
示例14: setConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Autowired
void setConverters(HttpMessageConverter<?>[] converters) {
this.mappingJackson2HttpMessageConverter = Arrays.stream(converters)
.filter(hmc -> hmc instanceof MappingJackson2HttpMessageConverter)
.findAny()
.get();
this.stringHttpMessageConverter = Arrays.stream(converters)
.filter(hmc -> hmc instanceof StringHttpMessageConverter)
.findAny()
.get();
Assert.assertNotNull("the JSON message converter must not be null",
this.mappingJackson2HttpMessageConverter);
}
示例15: configureMessageConverters
import org.springframework.http.converter.StringHttpMessageConverter; //導入依賴的package包/類
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
stringHttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
//避免IE執行AJAX時,返回JSON出現下載文件
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setDefaultCharset(Charset.forName("UTF-8"));
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(asList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML));
converters.add(byteArrayHttpMessageConverter);
converters.add(stringHttpMessageConverter);
converters.add(mappingJackson2HttpMessageConverter);
}