Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set encoding when use MappingJackson2HttpMessageConverter #26354

Closed
Clapmin opened this issue Jan 7, 2021 · 1 comment
Closed

How to set encoding when use MappingJackson2HttpMessageConverter #26354

Clapmin opened this issue Jan 7, 2021 · 1 comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)

Comments

@Clapmin
Copy link

Clapmin commented Jan 7, 2021

i have a code like below

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, "application/json;charset=EUC-KR");
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);

RestTemplate restTemplate = RestUtils.getRestTemplate(readTimeout);
Map response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(params, headers), Map.class).getBody();

Recently i updated spring 4.3* to 5.3*. After then it fail's to send request. Because, MappingJackson2HttpMessageConverter enforce encoding like UTF**(unicode)

@Override
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
    if (!canWrite(mediaType)) {
        return false;
    }
    if (mediaType != null && mediaType.getCharset() != null) {
        Charset charset = mediaType.getCharset();
        if (!ENCODINGS.containsKey(charset.name())) {
            return false;
        }
    }
    AtomicReference<Throwable> causeRef = new AtomicReference<>();
    if (this.objectMapper.canSerialize(clazz, causeRef)) {
        return true;
    }
    logWarningIfNecessary(clazz, causeRef.get());
    return false;
}
package com.fasterxml.jackson.core;

/**
 * Enumeration that defines legal encodings that can be used
 * for JSON content, based on list of allowed encodings from
 * <a href="http://www.ietf.org/rfc/rfc4627.txt">JSON specification</a>.
 *<p>
 * Note: if application want to explicitly disregard Encoding
 * limitations (to read in JSON encoded using an encoding not
 * listed as allowed), they can use {@link java.io.Reader} /
 * {@link java.io.Writer} instances as input
 */
public enum JsonEncoding {
    UTF8("UTF-8", false, 8), // N/A for big-endian, really
        UTF16_BE("UTF-16BE", true, 16),
        UTF16_LE("UTF-16LE", false, 16),
        UTF32_BE("UTF-32BE", true, 32),
        UTF32_LE("UTF-32LE", false, 32)
        ;
.......

how can i use 'euc-kr' as charset instead.

@Clapmin Clapmin changed the title How to overrind set encoding when use MappingJackson2HttpMessageConverter How to set encoding when use MappingJackson2HttpMessageConverter Jan 7, 2021
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 7, 2021
@poutsma
Copy link
Contributor

poutsma commented Jan 7, 2021

According to the relevant specifications, JSON should be written in UTF-8, though UTF-16 or UTF-32 can also be used. Therefore, Jackson (and the Spring components that depend on Jackson) do not support writing non-UTF JSON.

In earlier versions of Spring Framework, the message converters ignored the character encoding completely, and wrote UTF-8 even though the Content-Type indicated otherwise. See #25076

@poutsma poutsma closed this as completed Jan 7, 2021
@poutsma poutsma added in: web Issues in web modules (web, webmvc, webflux, websocket) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket)
Projects
None yet
Development

No branches or pull requests

3 participants