[BAEL-11601] Move and update Testing REST with multiple MIME types article code

Moved the code and cleaned spring-rest-full module.
This commit is contained in:
Ger Roza
2019-02-24 11:49:28 -03:00
parent 0f124ffd1c
commit 7ee231b817
12 changed files with 4 additions and 296 deletions
@@ -27,8 +27,7 @@ public class TestMarshallerFactory implements FactoryBean<IMarshaller> {
case "json":
return new JacksonMarshaller();
case "xml":
// If we need to implement xml marshaller we can include spring-rest-full XStreamMarshaller
throw new IllegalStateException();
return new XStreamMarshaller();
default:
throw new IllegalStateException();
}
@@ -0,0 +1,49 @@
package com.baeldung.test;
import java.util.List;
import com.baeldung.persistence.model.Foo;
import org.springframework.http.MediaType;
import com.google.common.base.Preconditions;
import com.thoughtworks.xstream.XStream;
public final class XStreamMarshaller implements IMarshaller {
private XStream xstream;
public XStreamMarshaller() {
super();
xstream = new XStream();
xstream.autodetectAnnotations(true);
xstream.processAnnotations(Foo.class);
}
// API
@Override
public final <T> String encode(final T resource) {
Preconditions.checkNotNull(resource);
return xstream.toXML(resource);
}
@SuppressWarnings("unchecked")
@Override
public final <T> T decode(final String resourceAsString, final Class<T> clazz) {
Preconditions.checkNotNull(resourceAsString);
return (T) xstream.fromXML(resourceAsString);
}
@SuppressWarnings("unchecked")
@Override
public <T> List<T> decodeList(final String resourcesAsString, final Class<T> clazz) {
return this.decode(resourcesAsString, List.class);
}
@Override
public final String getMime() {
return MediaType.APPLICATION_XML.toString();
}
}