Last active
August 29, 2015 13:57
-
-
Save jesslilly/9809171 to your computer and use it in GitHub Desktop.
Jetty, Jersey, Guice, JAXB Sample
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
// See http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html | |
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) | |
@XmlRootElement | |
public class Cake implements PlainTextBean { | |
private String type; | |
// The getter is up here since the order of fields affects the order in the | |
// xml. | |
public String getType() { | |
return type; | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
@XmlElement | |
public int height; | |
@XmlElement | |
public int radius; |
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
class CakeManagerImpl implements CakeManager { | |
public synchronized List<Cake> getCakes(String cakes) throws Exception { | |
return getCakes(Cake.parseCakeList(cakes)); | |
} | |
public synchronized List<Cake> getCakes(List<Cake> cakes) throws Exception { | |
ArrayList<Cake> a = new ArrayList<Cake>(); | |
for (Cake m : cakes) { | |
a.add(new Cake(m.getCake()).query()); | |
} | |
return a; | |
} |
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
@Path("/cakes") | |
public class CakeResource { | |
private final CakeManager manager; | |
@Inject | |
public CakeResource(CakeManager manager) { | |
this.manager = manager; | |
} | |
@GET @Path("recipes/{cakes}") | |
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN }) | |
public List<Cake> getCakes(@PathParam("cakes") String cakes) throws Exception { | |
Log.info("Called getCakes with cake(s) " + cakes); | |
cakes = Cake.removePrefix(cakes); | |
Log.info("Removed prefix from cake(s) " + cakes); | |
return manager.getCakes(cakes); | |
} |
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
@Provider | |
@Produces(MediaType.TEXT_PLAIN) | |
public class PlainTextWriter implements MessageBodyWriter<List<PlainTextBean>> { | |
@Override | |
public boolean isWriteable(Class<?> type, Type genericType, | |
Annotation[] annotations, MediaType mediaType) { | |
Log.info("Use PlainTextWriter to handle " + MediaType.TEXT_PLAIN); | |
// Use this writer for ANY text/plain. | |
return true; | |
} | |
@Override | |
public long getSize(List<PlainTextBean> myBean, Class<?> type, | |
Type genericType, Annotation[] annotations, MediaType mediaType) { | |
// TOOD: This is a performance hit. Not sure how else to do it right now. | |
return getData(myBean).length(); | |
} | |
@Override | |
public void writeTo(List<PlainTextBean> myBean, Class<?> type, | |
Type genericType, Annotation[] annotations, MediaType mediaType, | |
MultivaluedMap<String, Object> httpHeaders, | |
OutputStream entityStream) throws IOException, | |
WebApplicationException { | |
try { | |
// Convert any List<PlainTextBean> | |
// to a string by calling its toPlainText method. | |
// See | |
// http://stackoverflow.com/questions/22611105/create-a-text-plain-jersey-response | |
Log.info("Serializing a bean to text/plain."); | |
PrintStream p = new PrintStream(entityStream); | |
p.print(getData(myBean)); | |
p.flush(); | |
} catch (Exception e) { | |
Log.warn("Error serializing a List<PlainTextBean> to the output stream"); | |
Log.warn(e); | |
throw new WebApplicationException(500); | |
} | |
} | |
public String getData(List<PlainTextBean> list) { | |
StringBuilder text = new StringBuilder(); | |
for (PlainTextBean bean : list) { | |
text.append(bean.toPlainText()); | |
} | |
return text.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment