-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom objectMapper in JsonProvider (#12)
* feat: add custom objectMapper in JsonProvider * chore: add test for extra keys in request body * chore: update provider registration * feat: make registerProvider through objects protected * feat: enable custom json providers through interface * chore: update readme * chore: update readme
- Loading branch information
1 parent
a47953b
commit 66ff867
Showing
6 changed files
with
124 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,3 @@ | ||
package com.dream11.rest.provider; | ||
|
||
import com.dream11.rest.annotation.TypeValidationError; | ||
import com.dream11.rest.exception.RestException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import io.vertx.core.json.jackson.DatabindCodec; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
import lombok.SneakyThrows; | ||
import org.apache.http.HttpStatus; | ||
import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider; | ||
|
||
@Provider | ||
@Consumes({"application/json", "application/*+json", "text/json"}) | ||
@Produces({"application/json", "application/*+json", "text/json"}) | ||
public class JsonProvider extends ResteasyJackson2Provider { | ||
|
||
public JsonProvider() { | ||
this.setMapper(DatabindCodec.mapper()); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, | ||
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) { | ||
try { | ||
return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream); | ||
} catch (JsonMappingException e) { | ||
List<JsonMappingException.Reference> referenceList = e.getPath(); | ||
if (!referenceList.isEmpty()) { | ||
String fieldName = referenceList.get(0).getFieldName(); | ||
if (fieldName != null) { | ||
TypeValidationError typeValidationError = type.getDeclaredField(fieldName).getAnnotation(TypeValidationError.class); | ||
if (typeValidationError != null) { | ||
throw new RestException(typeValidationError.code(), typeValidationError.message(), | ||
typeValidationError.httpStatusCode(), e); | ||
} | ||
} | ||
} | ||
throw new RestException("INVALID_REQUEST", e.getMessage(), HttpStatus.SC_BAD_REQUEST, e); | ||
} catch (IOException e) { | ||
throw new RestException("INVALID_REQUEST", e.getMessage(), HttpStatus.SC_BAD_REQUEST, e); | ||
} | ||
} | ||
} | ||
public interface JsonProvider {} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/dream11/rest/provider/impl/JacksonProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.dream11.rest.provider.impl; | ||
|
||
import com.dream11.rest.annotation.TypeValidationError; | ||
import com.dream11.rest.exception.RestException; | ||
import com.dream11.rest.provider.JsonProvider; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.MultivaluedMap; | ||
import jakarta.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.util.List; | ||
import lombok.SneakyThrows; | ||
import org.apache.http.HttpStatus; | ||
import org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider; | ||
|
||
@Provider | ||
@Consumes({"application/json", "application/*+json", "text/json"}) | ||
@Produces({"application/json", "application/*+json", "text/json"}) | ||
public class JacksonProvider extends ResteasyJackson2Provider implements JsonProvider { | ||
|
||
public JacksonProvider(ObjectMapper mapper) { | ||
this.setMapper(mapper); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, | ||
MultivaluedMap<String, String> httpHeaders, InputStream entityStream) { | ||
try { | ||
return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream); | ||
} catch (JsonMappingException e) { | ||
List<JsonMappingException.Reference> referenceList = e.getPath(); | ||
if (!referenceList.isEmpty()) { | ||
String fieldName = referenceList.get(0).getFieldName(); | ||
if (fieldName != null) { | ||
TypeValidationError typeValidationError = type.getDeclaredField(fieldName).getAnnotation(TypeValidationError.class); | ||
if (typeValidationError != null) { | ||
throw new RestException(typeValidationError.code(), typeValidationError.message(), | ||
typeValidationError.httpStatusCode(), e); | ||
} | ||
} | ||
} | ||
throw new RestException("INVALID_REQUEST", e.getMessage(), HttpStatus.SC_BAD_REQUEST, e); | ||
} catch (IOException e) { | ||
throw new RestException("INVALID_REQUEST", e.getMessage(), HttpStatus.SC_BAD_REQUEST, e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters