java


Spring Boot MVC/Rest controller and enum deserialization converter


In my Spring Boot application I have a following #RestController method:
#RequestMapping(value = "/{decisionId}/decisions", method = RequestMethod.POST)
public List<DecisionResponse> getChildDecisions(#PathVariable Long decisionId, #Valid #RequestBody Direction direction) {
}
I use enum org.springframework.data.domain.Sort.Direction as a request body.
Right now the Spring internal logic unable to deserialize this Direction enum after request from client.
Could you please show how to write a custom enum converter(or something like that) and configure it with Spring Boot in order to be able to deserialize Direction enum from client request ? Also null value should be allowed.
First of all you should create custom converter class, that implements HttpMessageConverter<T> interface:
package com.somepackage;
public class DirectionConverter implements HttpMessageConverter<Sort.Direction> {
public boolean canRead(Class<?> aClass, MediaType mediaType) {
return aClass== Sort.Direction.class;
}
public boolean canWrite(Class<?> aClass, MediaType mediaType) {
return false;
}
public List<MediaType> getSupportedMediaTypes() {
return new LinkedList<MediaType>();
}
public Sort.Direction read(Class<? extends Sort.Direction> aClass,
HttpInputMessage httpInputMessage)
throws IOException, HttpMessageNotReadableException {
String string = IOUtils.toString(httpInputMessage.getBody(), "UTF-8");
//here do any convertions and return result
}
public void write(Sort.Direction value, MediaType mediaType,
HttpOutputMessage httpOutputMessage)
throws IOException, HttpMessageNotWritableException {
}
}
I used IOUtils from Apache Commons IO for convert InputStream to String. But you can do it any preferred way.
Now you have register created converter in Spring converters list. Add into <mvc:annotation-driven> tag next:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.somepackage.DirectionConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
Or if you're using java config:
#EnableWebMvc
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
messageConverters.add(new DirectionConverter());
super.configureMessageConverters(converters);
}
}

Related Links

SSL connectivity through certificates
Issue with HQL query
Can't retrieve JSON from Youtube API v3
Generate list of all possible unfinished tic-tac-toe positions in Java, considering symmetry [duplicate]
java regex catching space or &
I am getjava.sql.SQLException: After end of result set [closed]
Crash while creating Fragment View
Importing classes not working. “Unable to compile class for JSP”
autocomplete dictionary textfield implementation
How to set default value in #Entity object field
My app is getting the .apk file but when it's installed it says it has an application error
Java Swing KeyBindings stop working only on Mac
How to convert .doc or .docx file to .pdf file? [closed]
How can i get the specific data from array
facing compatability issues with seleniumwebdriver 2.48 and chromedriver2.9
RabbitMQ consumer thread pool increases with every request

Categories

HOME
winforms
variables
bluetooth
c#-4.0
push-notification
include
grep
octobercms
rsyslog
sqlite-net-extensions
icloud
cross-browser
ezpublish
add
google-translate
tomcat6
imacros
mapserver
usergrid
iolanguage
apache-metamodel
http-status-code-504
flux
contextmenu
viewport
jprofiler
grails3
object-detection
intentfilter
arabic
css-animations
telerik-reporting
bootstrap-tour
hammerspoon
crystal-reports-2010
qhull
cas
preg-match-all
functor
airconsole
code-contracts
rotational-matrices
mapdb
wixsharp
stacked
git-merge
swift3.0.2
service-discovery
c11
xenforo
angularjs-factory
lumberjack
unoconv
no-www
netcdf4
node-sass
wptoolkit
flickr-api
zip4j
react-scripts
dds
lowpass-filter
font-size
windows-iot-core-10
blacklist
savon
multiple-regression
bgp
historian
kendonumerictextbox
tcpserver
intel-fortran
mutation-observers
varargs
nsight
sniffer
iiviewdeckcontroller
unity5.2.3
notify
terminfo
contenttype
htmlcleaner
kgdb
aqtime
ftps
file-locking
smtp-auth
ocunit
delphi-6
dice
frameset
labwindows
punbb
libc++
joyent
cxxtest
sudzc
asp.net-mvc-areas
icanhaz.js
modelstate
rescale
sef
kdbg
simpletest
sector
post-build

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App