Skip to content

Instantly share code, notes, and snippets.

@Yosuke-Kawakami
Created February 15, 2017 07:27
Show Gist options
  • Save Yosuke-Kawakami/46282d86a9b4691187f43d3d687e28d0 to your computer and use it in GitHub Desktop.
Save Yosuke-Kawakami/46282d86a9b4691187f43d3d687e28d0 to your computer and use it in GitHub Desktop.
Using Jersey 2.x , use Interface
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
// [!!] Jersey is trying to instantiate the interface, which it can't.
// @Path("/hoge")
public interface SampleClass
{
@GET
@Path("/p1")
@Produces(MediaType.TEXT_PLAIN + "; charset=UTF-8")
public String saySomething();
@GET
@Path("/p1/{message}")
@Produces(MediaType.TEXT_PLAIN + "; charset=UTF-8")
public String saySomething(@PathParam("message") String message);
@POST
@Path("/p1")
@Produces(MediaType.APPLICATION_FORM_URLENCODED)
public String postedParams(
@FormParam ("val1") String val1,
@FormParam ("val2") String val2
);
}
import javax.ws.rs.Path;
import myclasses.SampleClass;
@Path("/hoge")
public class SampleClassImp implements SampleClass
{
@Override
public String saySomething()
{
return "what?";
}
@Override
public String saySomething(String message)
{
return String.format("%s", message);
}
@Override
public String postedParams(String val1, String val2)
{
return String.format("val1 : %s , val2 : %s", val1, val2);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>PracticeForWebAPIs</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>myclasses</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment