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

Expose protected method to determine charset in AbstractJackson2HttpMessageConverter #25509

Closed
nikomiranda opened this issue Aug 3, 2020 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: task A general task
Milestone

Comments

@nikomiranda
Copy link

nikomiranda commented Aug 3, 2020

When the issue of charset being ignored #25076 was resolved on 5.2.7/5.2.8 the problem now is the opposite. I have lot of clients that are sending ;charset=ISO-8859-1 on their request (for example by using RestTemplate is added by default), and know request and responses in Japanese language are garbled (should be UTF-8)

A simple way to ignore the charset by configuration or at least making some members protected would be nice.

Now the only alternative is to override complete methods

public class ForcedUTF8MappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
	
	public ForcedUTF8MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
		super(objectMapper);
	}

	@Override
	public Object read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
			throws IOException, HttpMessageNotReadableException {

		JavaType javaType = getJavaType(type, contextClass);
		return readJavaType(javaType, inputMessage);
	}

	private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) throws IOException {
		Charset charset = StandardCharsets.UTF_8;

		boolean isUnicode = true;
		try {
			if (inputMessage instanceof MappingJacksonInputMessage) {
				Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
				if (deserializationView != null) {
					ObjectReader objectReader = this.objectMapper.readerWithView(deserializationView).forType(javaType);
					if (isUnicode) {
						return objectReader.readValue(inputMessage.getBody());
					}
					else {
						Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
						return objectReader.readValue(reader);
					}
				}
			}
			if (isUnicode) {
				return this.objectMapper.readValue(inputMessage.getBody(), javaType);
			}
			else {
				Reader reader = new InputStreamReader(inputMessage.getBody(), charset);
				return this.objectMapper.readValue(reader, javaType);
			}
		}
		catch (InvalidDefinitionException ex) {
			throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex);
		}
		catch (JsonProcessingException ex) {
			throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex, inputMessage);
		}
	}

	@Override
	protected JsonEncoding getJsonEncoding(@Nullable MediaType contentType) {
		return JsonEncoding.UTF8;
	}	

}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 3, 2020
@rstoyanchev
Copy link
Contributor

We could make the getCharset method protected.

@rstoyanchev rstoyanchev added in: web Issues in web modules (web, webmvc, webflux, websocket) type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 13, 2020
@rstoyanchev rstoyanchev self-assigned this Aug 13, 2020
@rstoyanchev rstoyanchev added this to the 5.2.9 milestone Aug 13, 2020
@rstoyanchev rstoyanchev changed the title Easy way to ignore charset on Content-Type Expose protected method to determine charset in AbstractJackson2HttpMessageConverter Aug 13, 2020
@jhoeller jhoeller added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Sep 3, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x labels Sep 3, 2020
jhoeller added a commit that referenced this issue Sep 8, 2020
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
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) status: backported An issue that has been backported to maintenance branches type: task A general task
Projects
None yet
Development

No branches or pull requests

4 participants