如何在同一个网络服务中获取@PathParam 和请求体?
How to get @PathParam and request body in same web-services?
我正在调用服务并希望映射来自路径参数和请求正文的数据。 Web 服务调用的类型为 POST。
这里的问题是,如果我使用@Form,那么我只会得到路径参数
这是场景。
Case1 Url :- Response updateDepartment(@Form @Valid Department departmentObject)
我得到的路径参数值为 departmentNumber、departmentSNumber 和 departmentName
但没有得到包含 addRequest[{ }] 和 removeRequest[{ }]
的请求正文
如果我只使用@Valid 那么
Case 2 Url :- Response updateDepartment(@Valid Department departmentObject)
在这种情况下,我得到了 addRequest[{ }] 和 removeRequest[{ }] 值。
但是对象,但现在缺少 departmentNumber、departmentSNumber 和 departmentName
Department.java
public class Department
{
@PathParam(DEPTNUMBER_PARAM)
private String deptNumber;
@PathParam(DEPT_S_PARAM)
private String deptSNumber;
@PathParam(DEPARTMENT_NAME)
private String departmentName;
private List<String> addRole;
private List<String> removeRole;
}
如何映射@PathParam 形式的值以及请求正文值。
这是调用
http://localhost:8080/myweb/departments/1234/4321/finance
{
"addRole": [
101,
181
],
"removeRole": [
42,
12
]
}
你为什么不用这个:
Response updateDepartment(@Valid Department departmentObject, @PathParam("departmentNumber") String departmentNumber....)
你想要的是行不通的。我会尽力解释。
首先,我认为您使用的是 resteasy 而不是 JAX-RS 参考,对吗?
在 @Form 注释的 RestEasy 中它说:
This can be used as a value object for incoming/outgoing request/responses. You can re-use @*Param annotations on fields/methods of the parameter to unmarshall from the request or marshall to the response depending if you're using server-side JAX-RS or the Resteasy client framework
在你的例子中,你在参数的字段上使用了@PathParam,这就是填充的原因。
当您不使用@Form 时,无论您为参数指定什么类型,它都会尝试与 请求主体 匹配,在您的情况下,它只包含两个数组,而不包含路径参数。它不会查看其他任何路径参数,因此会忽略您的注释。
我正在调用服务并希望映射来自路径参数和请求正文的数据。 Web 服务调用的类型为 POST。 这里的问题是,如果我使用@Form,那么我只会得到路径参数 这是场景。
Case1 Url :- Response updateDepartment(@Form @Valid Department departmentObject)
我得到的路径参数值为 departmentNumber、departmentSNumber 和 departmentName 但没有得到包含 addRequest[{ }] 和 removeRequest[{ }]
的请求正文如果我只使用@Valid 那么
Case 2 Url :- Response updateDepartment(@Valid Department departmentObject)
在这种情况下,我得到了 addRequest[{ }] 和 removeRequest[{ }] 值。 但是对象,但现在缺少 departmentNumber、departmentSNumber 和 departmentName
Department.java
public class Department
{
@PathParam(DEPTNUMBER_PARAM)
private String deptNumber;
@PathParam(DEPT_S_PARAM)
private String deptSNumber;
@PathParam(DEPARTMENT_NAME)
private String departmentName;
private List<String> addRole;
private List<String> removeRole;
}
如何映射@PathParam 形式的值以及请求正文值。 这是调用
http://localhost:8080/myweb/departments/1234/4321/finance
{
"addRole": [
101,
181
],
"removeRole": [
42,
12
]
}
你为什么不用这个:
Response updateDepartment(@Valid Department departmentObject, @PathParam("departmentNumber") String departmentNumber....)
你想要的是行不通的。我会尽力解释。 首先,我认为您使用的是 resteasy 而不是 JAX-RS 参考,对吗? 在 @Form 注释的 RestEasy 中它说:
This can be used as a value object for incoming/outgoing request/responses. You can re-use @*Param annotations on fields/methods of the parameter to unmarshall from the request or marshall to the response depending if you're using server-side JAX-RS or the Resteasy client framework
在你的例子中,你在参数的字段上使用了@PathParam,这就是填充的原因。
当您不使用@Form 时,无论您为参数指定什么类型,它都会尝试与 请求主体 匹配,在您的情况下,它只包含两个数组,而不包含路径参数。它不会查看其他任何路径参数,因此会忽略您的注释。