javax.​ws.​rs

public class RestJaxRsServer {
    
    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8009);
        component.getDefaultHost().attach(new RestJaxRsApplication(null));
        component.start();
        
        System.out.println("The restlet server started ...");
    }
}
public class RestJaxRsApplication extends JaxRsApplication {

    public RestJaxRsApplication(Context context) {
        super(context);
        this.add(new MyApplication());
    }

}
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<Class<?>>();
        
        resources.add(StudentResource.class);
        resources.add(TeacherResource.class);
        resources.add(TestResource.class);
        
        return resources;
    }
    

}
@Path("/test")
public class TestResource {    
    @GET
    @Path("test")
    @Produces("text/html")  public String test(@QueryParam("name") String name,@DefaultValue("10") @QueryParam("age") int age) throws InterruptedException {
        System.out.println("name="+name);
        System.out.println("age="+age);
        return "SUCCESS";
    }
    
}

1、概述

@Consumes 注释代表的是一个资源可以接受的 MIME 类型。

@Produces 注释代表的是一个资源可以返回的 MIME 类型。

这些注释均可在资源、资源方法、子资源方法、子资源定位器或子资源内找到。

常见几个注解

@Cookieparam

@FormParam

@HeaderParam

@MatrixParam

@PathParam

@QueryParam

@BeanParam

总共7个注解。

这里我先介绍下以下三个注解

@MatrixParam

@PathParam

@QueryParam

这三个注解都是从URL里面取东西的。

@MatrixParamURL分号(;)后面的参数。这个也要注意,一定要在?号前面。

@PathParamURL路径里的参数。使用的时候需要注意,要在@Path注解内放一个变量,用{}括起来,然后才可以使用。

@QueryParamURL?后面的请求参数。

2、@Produces:返回的类型

a.返回给client字符串类型(text/plain)

@Produces(MediaType.TEXT_PLAIN)   

b.返回给client为json类型(application/json)

@Produces(MediaType.APPLICATION_JSON)   

3、@Consumes

@Consumes@Produces相反,用来指定可以接受client发送过来的MIME类型,同样可以用于class或者method,也可以指定多个MIME类

型,一般用于@PUT,@POST

a.接受client参数为字符串类型

@Consumes(MediaType.TEXT_PLAIN)  

b.接受clent参数为json类型@Consumes(MediaType.APPLICATION_JSON)

其他注解:

@PathParam
    @GET    
    @Path("{username"})    
    @Produces(MediaType.APPLICATION_JSON)    
    public User getUser(@PathParam("username") String userName) {    
        ...    
    }    
@QueryParam
获取get请求中的查询参数: 
    @GET    
    @Path("/user")    
    @Produces("text/plain")    
    public User getUser(@QueryParam("name") String name,    
                         @QueryParam("age") int age) {    
        ...    
    }   
 
如果需要为参数设置默认值,可以使用@DefaultValue,如:
 
    @GET    
    @Path("/user")    
    @Produces("text/plain")    
    public User getUser(@QueryParam("name") String name,    
                        @DefaultValue("26") @QueryParam("age") int age) {    
        ...    
    }  
 
@FormParam
 获取post请求中表单中的数据:
    @POST    
    @Consumes("application/x-www-form-urlencoded")    
    public void post(@FormParam("name") String name) {    
        // Store the message    
    }   
@BeanParam
 获取请求参数中的数据,用实体Bean进行封装
    @POST    
    @Consumes("application/x-www-form-urlencoded")    
    public void update(@BeanParam User user) {    
        // Store the user data    
    }    
 
 
原文地址:https://www.cnblogs.com/yangqimo/p/8022323.html