Skip to content

Instantly share code, notes, and snippets.

@uemuraj
Created October 16, 2012 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uemuraj/3896968 to your computer and use it in GitHub Desktop.
Save uemuraj/3896968 to your computer and use it in GitHub Desktop.
JAX-RS (Jersey) を利用した JSON/XML 両対応の簡単な Web サービスの実装
#!/usr/bin/env groovy
//
// http://weblogs.java.net/blog/lamineba/archive/2012/05/20/building-restful-web-services-jax-rs-jaxb-and-groovy
//
@Grab('com.sun.jersey:jersey-core')
@Grab('com.sun.jersey:jersey-json')
@Grab('com.sun.jersey:jersey-server')
import com.sun.jersey.api.container.ContainerFactory
@Grab('javax.ws.rs:jsr311-api')
import javax.ws.rs.*
import javax.ws.rs.core.*
// JDK 1.6+
import com.sun.net.httpserver.*
// JDK 1.6+
import javax.xml.bind.annotation.*
HttpHandler handler = ContainerFactory.createContainer(HttpHandler.class, Message.class)
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0)
server.createContext("/basic", handler)
server.setExecutor(null)
server.start()
@Path("/planets")
public class Message {
@GET
@Path('/earth')
@Produces([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
public Planet getMessage() {
return new Planet(id:1L, name:'Earth', radius:1.0)
}
}
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement
public class Planet {
@XmlElement long id
@XmlElement String name
@XmlElement double radius
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment