Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍

Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍

@GET

http请求方式里面的get请求,标记这个请求方式为get

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* http://localhost:8080/v1/province/city/1
* @param id
* @return
*/
@Path("city/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON) //表示输出json
@Consumes(MediaType.APPLICATION_JSON) //表示输入为json
public Result<Boolean> getCity(@PathParam("id") Long id) {
return new Result<>();
}

@PUT

http请求方式里面的get请求,标记这个请求方式为put

一般用来创建一个资源

1
2
3
4
5
6
7
8
@Path("city/{id}")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> getCity(@PathParam("id") Long id) {
return new Result<>();
}

###@POST

http请求方式里面的get请求,标记这个请求方式为post

一般用来创建和更新

1
2
3
4
5
6
7
8
@Path("city/{id}")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> postCity(@PathParam("id") Long id) {
return new Result<>();
}

@DELETE

http请求方式里面的get请求,标记这个请求方式为delete

一般用来删除一个资源

1
2
3
4
5
6
7
@Path("city/{id}")
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> postCity(@PathParam("id") Long id) {
return new Result<>();
}

@PathParam

一般url里面的参数用PathParam

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* http://localhost:8080/v1/province/city/1
* @param id
* @return
*/
@Path("city/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON) //表示输出json
@Consumes(MediaType.APPLICATION_JSON) //表示输入为json
public Result<Boolean> getCity(@PathParam("id") Long id) {
return new Result<>();
}

@QueryParam

可以输出url后面的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* http://localhost:8080/v1/province/queryCity/1?name=wh
* @param id
* @return
*/
@Path("queryCity/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> queryCity(@PathParam("id") Long id,@QueryParam("name")String name) {
System.out.println(name);
return new Result<>();
}

@HeaderParam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* http://localhost:8080/v1/province/queryCity/1?name=wh
* @param id
* @return
*/
@Path("queryCity/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> queryCity(@HeaderParam("auth")String authKey,@PathParam("id") Long id,@QueryParam("name")String name) {
System.out.println(name);
System.out.println(authKey);
return new Result<>();
}

效果如下图

@CookieParam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* http://localhost:8080/v1/province/queryCity/1?name=wh
* @param id
* @return
*/
@Path("queryCity/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> queryCity(@CookieParam("__utma")String sessionId,@HeaderParam("auth")String authKey,@PathParam("id") Long id,@QueryParam("name")String name) {
System.out.println(name);
System.out.println(sessionId);
System.out.println(authKey);
return new Result<>();
}

注: _utma是postman里面自带的一个cookie,所以直接调用就可以拿到值,不用额外设置

@MatrixParam

matrixParam一般是用来分离参数的,一般和分号一起使用,下例中,分号后面的from被成功识别打印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* http://localhost:8080/v1/province/queryCity/1;from=wh?name=sh
* @param id
* @return
*/
@Path("queryCity/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> queryCity(@MatrixParam("from")String from,@CookieParam("__utma")String sessionId,@HeaderParam("auth")String authKey,@PathParam("id") Long id,@QueryParam("name")String name) {
System.out.println(id);
System.out.println(name);
System.out.println(sessionId);
System.out.println(authKey);
System.out.println(from);
return new Result<>();
}
1
2
3
4
5
1
sh
111872281.1804816201.1493794349.1495509065.1496976869.12
xsdfarewrsdfjnainrfa;ewe==
wh

@FormParam

postman里面改为post请求,表单中添加age参数,得到输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* http://localhost:8080/v1/province/city/1?name=%22%E5%B0%8F%E6%98%8E%22
* @param id
* @param name
* @return
*/
@POST
@Path("city/{id}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Result<Boolean> addBook(@FormParam("age")Integer age,@PathParam("id") String id, @QueryParam("name") String name) {
System.out.println(id);
System.out.println(name);
System.out.println(age);
return new Result<>();
}
1
2
3
1
sh
23

###@Form

这个注解,允许你将你的form注入到一个实体里面,看例子吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MyForm {
@FormParam("age")
private int age;
@HeaderParam("auth")
private String authKey;
@PathParam("id")
private String id;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setAuthKey(String authKey) {
this.authKey = authKey;
}
public String getAuthKey() {
return authKey;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
1
2
3
4
5
6
7
8
@PUT
@Path("city/{id}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Result<Boolean> addForm(@Form MyForm form) {
System.out.println(JSON.toJSONString(form));
return new Result<>();
}

输出

1
{"age":23,"authKey":"xsdfarewrsdfjnainrfa;ewe==","id":"1"}

同样也可以把@Form注入到一个实体里面去

@DefaultValue

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* http://localhost:8080/v1/province/queryCity/1?name=wh
* @param id
* @return
*/
@Path("queryCity/{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> queryCity(@PathParam("id") Long id,@DefaultValue("sh")@QueryParam("name")String name) {
System.out.println(name);
return new Result<>();
}

可以在queryParam前面加一个默认值

@Context

1
2
3
4
5
6
7
8
9
10
The @Context 注解允许你注入下面这些类
• javax.ws.rs.core.HttpHeaders
• javax.ws.rs.core.UriInfo
• javax.ws.rs.core.Request
• javax.servlet.http.HttpServletRequest
• javax.servlet.http.HttpServletResponse
• javax.servlet.ServletConfig
• javax.servlet.ServletContext
• javax.ws.rs.core.SecurityContext
在filter中使用较多

@Encoded and encoding

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* http://localhost:8080/v1/province/en_city/小明
* @param name
* @return
*/
@Path(value = "en_city/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Result<Boolean> getCity(@PathParam("name")@Encoded String name) {
System.out.println(name);
return new Result<>();
}

输出

1
%E5%B0%8F%E6%98%8E

end

Compartir