1、在maven配置文件中需要添加如下信息
<!-- spring-boot-starter-jersey --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jersey</artifactId> </dependency>
2、实现ResourceConfig类对指定的包进行扫描注册指定的接口。并将该实现类注册到SpringBoot容器中。
package com.kingdee.config; import javax.ws.rs.ApplicationPath; import org.glassfish.jersey.server.ResourceConfig; import org.springframework.stereotype.Component; import com.kingdee.filter.MyContainerRequestFilter; import com.kingdee.filter.MyContainerResponseFilter; @Component @ApplicationPath("/microblog") public class MicroblogResourceConfig extends ResourceConfig{ public MicroblogResourceConfig() { System.out.println("000000000000000000000"); packages("com.kingdee.controller.rest"); register(MyContainerRequestFilter.class); register(MyContainerResponseFilter.class); } }
3、设置接口
package com.kingdee.controller.rest; import java.awt.List; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.util.Date; import java.util.LinkedList; import javax.jws.soap.SOAPBinding.Use; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.websocket.server.PathParam; import javax.ws.rs.BeanParam; import javax.ws.rs.Consumes; import javax.ws.rs.CookieParam; import javax.ws.rs.DefaultValue; import javax.ws.rs.FormParam; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.HttpHeaders; import org.apache.coyote.http11.filters.BufferedInputFilter; import org.springframework.boot.autoconfigure.security.SecurityProperties.Headers; import org.springframework.stereotype.Component; import com.kingdee.annotation.MyBindingFilter; import com.kingdee.entity.User; import com.sun.research.ws.wadl.Request; @Component @Path("api") public class MicroblogController { /** * @QueryParam为主要通过键值对取参数值 * @param request * @param id * @param name * @param gender * @return */ @GET//以GET的方式进行接口调用 @Path("find")//接口路径 @Produces("applications/json") public Object publicMicroblog(@Context HttpServletRequest request, @QueryParam("id") @DefaultValue("") String id,//主要通过键值对这样取 @QueryParam("name") @DefaultValue("") String name, @QueryParam("gender") @DefaultValue("0") int gender){ User user = new User(); user.setName(name); user.setId(id); user.setGender(gender); System.out.println("GET=============="+user.getName()); return user; } @POST//以POST的方式进行接口构建 @Path("update")//接口路径 @Produces("application/json") public Object updateMicroblog(@Context HttpServletRequest request, @QueryParam("id") @DefaultValue("") String id, @QueryParam("name") @DefaultValue("") String name, @QueryParam("gender") @DefaultValue("0") int gender){ User user = new User(); user.setName(name); user.setId(id); user.setGender(gender); System.out.println("POST=================="+user.getName()); return user; } /** * @PathParam将参数作为接口路径的一部分进行组合 */ @Path("comment/{name}") @POST @Produces("application/json") public Object commentByName(@PathParam("name") String name, @QueryParam("id") String id,@QueryParam("gender") int gender){ User user = new User(); user.setName(name); user.setId(id); user.setGender(gender); System.out.println("@PathParam=================="+user.getName()); return user; } /** * @FormParam将以表单的形式传递参数 */ @Path("commentupdate") @POST @Produces("application/json") public Object updateComment(@FormParam("name") String name, @FormParam("id") String id,@FormParam("gender") int gender){ User user = new User(); user.setName(name); user.setId(id); user.setGender(gender); System.out.println("@FormParam=================="+user.getName()); return user; } /** * @BeanParam将自定义一个参数组合实例 */ @Path("savecomment") @POST @Produces("application/json") public Object saveComment(@BeanParam User user){ System.out.println("@BeanParam=================="+user.getName()); return user; } /** * @CookieParam匹配cookie中的键值对 */ @Path("findcomments") @POST @Produces("application/json") public LinkedList<User> findComments(@CookieParam("name") String name, @CookieParam("id") String id){ User user = new User(); user.setId(id); user.setName(name); System.out.println("@CookieParam=================="+user.getName()); LinkedList<User> list = new LinkedList<User>(); list.add(user); return list; } /** * @Context注解来获取上下文参数(ServletContext,ServletRequest,ServletResponse),获取请求头信息,获取请求处理信息, */ @Path("savemicroblog") @POST @Produces("application/json") public void saveMicroblog(@Context HttpServletRequest request, @Context HttpHeaders headers,@Context ServletContext ctx, @Context Request request2){ String name = (String) request.getAttribute("name"); System.out.println("@Context========httpservletrequest=========="+name); System.out.println("@Context========headers=========="+headers.getHeaderString("token")); System.out.println("@Context========servletcontext=========="+ctx); System.out.println("@Context========request=========="+request2); } /** * 传入文件类型 * @throws IOException */ @Path("uploadfile") @POST public void uploadfile(File file) throws IOException{ try(BufferedReader br = new BufferedReader(new FileReader(file))){ String s; do { s = br.readLine(); System.out.println("========file=========="+s); } while (s != null); } } /** * 参数为InputStream类型 * @throws IOException */ @Path("input") @POST public void inputfile(InputStream inputStream) throws IOException{ try(BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))){ String s; do { s = br.readLine(); System.out.println("========inputStream=========="+s); } while (s != null); } } /** * 参数为reader类型 * @throws IOException */ @Path("reader") @POST public void readerfile(Reader reader) throws IOException{ try(BufferedReader br = new BufferedReader(reader)){ String s; do { s = br.readLine(); System.out.println("========inputStream=========="+s); } while (s != null); } } /** * @Consumes将参数以一定的格式进行接收 * @Produces将结果以一定的格式进行输出 */ @MyBindingFilter @Path("json") @POST @Consumes("application/json") @Produces("application/json") public User findMicroblogByName(User user){ User user1 = new User(); user1.setName(user.getName()); user1.setOrgLongName(user.getOrgLongName()); System.out.println("===========json========="+user.getName()); return user; } }