Skip to content

Commit

Permalink
Merge pull request #575 from VictorZeng/master
Browse files Browse the repository at this point in the history
Fix issue #574 add compatible with spring framework 4.2+.
  • Loading branch information
wenshao committed Apr 24, 2016
2 parents 8ec22ed + a985b60 commit ba143c1
Showing 1 changed file with 47 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public FastJsonHttpMessageConverter() {
super(MediaType.APPLICATION_JSON, MediaType.APPLICATION_FORM_URLENCODED);
}

@Override
protected boolean supports(Class<?> clazz) {
return true;
}

public Charset getCharset() {
return this.charset;
}
Expand Down Expand Up @@ -82,6 +77,11 @@ public void setFilters(SerializeFilter... filters) {
this.filters = filters;
}

@Override
protected boolean supports(Class<?> clazz) {
return true;
}

@Override
protected Object readInternal(Class<? extends Object> clazz,
HttpInputMessage inputMessage) throws IOException,
Expand Down Expand Up @@ -122,7 +122,7 @@ protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
headers.setContentLength(bytes.length);
OutputStream out = outputMessage.getBody();
out.write(bytes);
out.flush();
// out.flush();
}

public void addSerializeFilter(SerializeFilter filter) {
Expand All @@ -137,13 +137,25 @@ public void addSerializeFilter(SerializeFilter filter) {
this.filters = filters;
}

@Override
/*
* @see org.springframework.http.converter.GenericHttpMessageConverter#canRead(java.lang.reflect.Type, java.lang.Class, org.springframework.http.MediaType)
*/
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {

return super.canRead(type.getClass(), mediaType);
return super.canRead(contextClass, mediaType);
}

@Override
/*
* @see org.springframework.http.converter.GenericHttpMessageConverter#canWrite(java.lang.reflect.Type, java.lang.Class, org.springframework.http.MediaType)
*/
public boolean canWrite(Type type, Class<?> contextClass, MediaType mediaType) {

return super.canWrite(contextClass, mediaType);
}

/*
* @see org.springframework.http.converter.GenericHttpMessageConverter#read(java.lang.reflect.Type, java.lang.Class, org.springframework.http.HttpInputMessage)
*/
public Object read(Type type, Class<?> contextClass,
HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException {
Expand All @@ -170,4 +182,30 @@ public Object read(Type type, Class<?> contextClass,
type);
}

/*
* @see org.springframework.http.converter.GenericHttpMessageConverter#write(java.lang.Object, java.lang.reflect.Type, org.springframework.http.MediaType, org.springframework.http.HttpOutputMessage)
*/
public void write(final Object t, Type type, MediaType contentType,
HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {

HttpHeaders headers = outputMessage.getHeaders();
if (headers.getContentType() == null) {
if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentType = getDefaultContentType(t);
}
if (contentType != null) {
headers.setContentType(contentType);
}
}
if (headers.getContentLength() == -1) {
Long contentLength = getContentLength(t, headers.getContentType());
if (contentLength != null) {
headers.setContentLength(contentLength);
}
}
writeInternal(t, outputMessage);
outputMessage.getBody().flush();
}

}

0 comments on commit ba143c1

Please sign in to comment.