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

Pass data to action class using Ajax
How to get last value from the list and store in another
How to create a tar.gz file from a list of files?
What is wrong with this Java code to handle ArrayList? [duplicate]
Why doesn't my JavaFX UI update when I add or remove nodes in runtime?
How to stick a JDialog to a JFrame?
Array in get parameter Rest Assured
Bad characters when replacing text in pdf using pdfbox
Why is Scriptlet syntax shown in the browser when the server is Red hat Linux 5?
How to get Java Drawables from a Plugin in Unity
How do I use maths and a 2d graph using random numbers in Java
How to utilise OSGi-loaded classes in a program after starting their source bundle
Pressing enter key does not select checkbox
Unable to get total to decrease
Maven exclude resources not working
Tomcat 8.0.x Websockets, SSL and Windows --> last frame delayed

Categories

HOME
xamarin
gitlab
reflection
azure-data-factory
ngrx
view
include
vmware
adb
amortized-analysis
v8
ckeditor
retrofit
in-app-purchase
paradox
adfs
applepay
spring-xd
timeout
vb.net-2010
phaser
finite-automata
footer
invantive-sql
oxyplot
shared-hosting
predix
immutable.js
reverse-proxy
intentfilter
accessor
chromium-embedded
traffic
greendao
windows-server-2000
skia
sql-server-2012-express
socialengine
nameservers
data-manipulation
catch-all
repo
git-merge
jquery-bootgrid
android-browser
starteam
fedex
ruby-on-rails-3.1
powershell-dsc
http-live-streaming
keydown
unixodbc
thin
angular2-meteor
avconv
react-scripts
dotnetzip
togetherjs
elgg
drupal-6
carrot
imanage
hittest
vtigercrm
pycaffe
root-framework
sigabrt
testng-dataprovider
make-install
pickadate
dlna
hexdump
php-parse-error
wapiti
phpcas
lemon
collapse
feedback
p2
device-orientation
lustre
sorl-thumbnail
coldbox
phalanger
ceil
aapt
uitouch
stxxl
gnu-smalltalk
sitemesh
spring-io
smtp-auth
twrequest
reddot
bigcouch
data-loss
iphone-web-app
site.master
compiler-specific
webkit.net
yagni
thread-local-storage
gacutil
spec#
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App