web.xml
RestAPIDemo.javaRESTEasyDemo resteasy.scan true resteasy.servlet.mapping.prefix /rest Resteasy org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher Resteasy /rest/*
package demo; import javax.ws.rs.Consumes; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("demo") public class RestAPIDemo { public RestAPIDemo() { } @GET public Response demo() { // http://localhost:8080/RESTEasyDemo/rest/demo System.out.println("demo start .."); return Response.status(200).entity("demo ok").build(); } @GET @Path("/pathParamDemo/{pathParam}") public Response pathParamDemo(@PathParam("pathParam") String pathParam) { // http://localhost:8080/RESTEasyDemo/rest/demo/pathParamDemo/123 System.out.println("get pathParam : " + pathParam); return Response.status(200).entity("get pathParam : " + pathParam).build(); } @GET @Path("/queryParamDemo") public Response queryParamDemo(@DefaultValue("This id DefaultValue") @QueryParam("queryParam") String queryParam) { // http://localhost:8080/RESTEasyDemo/rest/demo/queryParamDemo?queryParam=123 System.out.println("get queryParam : " + queryParam); return Response.status(200).entity("get queryParam : " + queryParam).build(); } @POST @Path("/getPostBody") public Response getPostBody(String postBody) { // http://localhost:8080/RESTEasyDemo/rest/demo/getPostBody // request body => 123467 System.out.println("get postBody : " + postBody); return Response.status(200).entity("get postBody : " + postBody).build(); } @POST @Path("/getUserInfo") @Consumes("application/json") public Response getUserInfo(UserInfo userInfo) { // http://localhost:8080/RESTEasyDemo/rest/demo/getUserInfo // header => Content-type:application/json // request body => // { // "name": "myName", // "mail": "mymail@mail.com", // "phone": "0988888888" // } System.out.println("get UserInfo : " + userInfo); return Response.status(200).entity("get UserInfo : " + userInfo).build(); } }UserInfo.java
package demo; public class UserInfo { public UserInfo() { } private String name; private String mail; private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "UserInfo [ name = " + name + " , mail = " + mail + " , phone = " + phone+ " ]"; } }
沒有留言:
張貼留言