I have embedded jetty server I want to create RESTful GET service which returns a pojo in XML/JSON format as response. can anyone give me one basic example how to write the handler for jetty? the example given only shows text type output.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
I suggest you use Jersey java REST framework (http://jersey.java.net/). The framework is easy to learn. You can use Object to Xml converter like JAXB to make your life easier.
Hmm. I had the same problem. I solved it by having a utility jar file that reads a properties file to configure contexts for Jersey Servlets, handlers, static files, exploded webapps etc in such a way that the resulting application jar configures the contexts automagically and is run from the command line.
Basically I have a HandlerCollection and successively add the servlets to it.
Then I have an example Jersey servlet:
}
The first calls gets a perf hit as Jersey configures stuff, but it works just peachy.
A junit test looks like this:
CURL calls look like this:
Er... The following is not a plug. Seriously. It might be refused by the company...
I have a complete solution by which 1 jar file is added as a dependency and several tiny files (app.properties, classpath.sh, log4j.properties and run.sh) that completely configure a Jetty8 instance for numerous contexts, Handlers, Servlets, JerseyServlets, StaticFiles and ExplodedWebApps. The result is a self contained executable Jar that restarts, reloads, stops etc with almost zero effort. An added benefit is that it can act as a pseudo-classloader and avoids jar-hell. (A side effect is that mvn clean test works against it also)
If any one is interested, ping me and I can see if the company will allow me to OpenSource it and get it up on GitHub. Or perhaps even document it via my own site http://www.randomactsofsentience.com
Just FYI on embedded Jetty in general... I have created a github project that I humbly submit may cover most of the embedded jetty issues that keep cropping up. See https://github.com/ZenGirl/EmbeddedJettyRepository for details.