Jersey只从@BeanParam参数中读取第一个@QueryParam

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jersey只从@BeanParam参数中读取第一个@QueryParam相关的知识,希望对你有一定的参考价值。

我有这个Jersey POST资源:

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public Response blablabla(InputStream inputStream, 
@BeanParam ImportParams importParams) throws IOException, JAXBException, SAXException {

这是ImportParams类:

public class ImportParams {
    @QueryParam(value = "importType")
    public ImportType importType = ImportType.MERGE;

    @ApiParam("Force stop point type for all stop points in file. Useful if no modality defined in the netex file.")
    @QueryParam(value = "forceStopType")
    public StopTypeEnumeration forceStopType;

}

当我使用curl发布到资源时,只有我在URL中的问号后指定的第一个查询参数由jersey读取:

curl -XPOST -H"Content-Type: application/xml" -H"authorization: bearer $TOKEN" -d@$3  http://localhost:8585/services/stop_places/netex?forceStopType=TRAM_STATION&importType=MERGE

==> forceStopType has the right value, and importType is null

curl -XPOST -H"Content-Type: application/xml" -H"authorization: bearer $TOKEN" -d@$3  http://localhost:8585/services/stop_places/netex?importType=MERGE&forceStopType=TRAM_STATION

==> importType has the right value and forceStopType is null

我以前曾经多次使用过@BeanParam而且它曾经工作过,所以我一定会错过一些明显的东西....感谢您的帮助

答案

哈哈发现了 - 愚蠢的我。当cURLing时,不得不在URL周围加上双引号:

curl -XPOST -H"Content-Type: application/xml" -H"authorization: bearer $TOKEN" -d@$3  "http://localhost:8585/services/stop_places/netex?forceStopType=TRAM_STATION&importType=MERGE"

以上是关于Jersey只从@BeanParam参数中读取第一个@QueryParam的主要内容,如果未能解决你的问题,请参考以下文章

JAX-RS Jersey 读取内容类型为“*”的实体

使用Jersey的Http DELETE参数

Jersey写Restful接口获取参数的问题

Jersey REST 服务调用另一个 Jersey REST 服务时出错 - java.lang.IllegalArgumentException:参数类型不匹配

Jersey 2.x 使用 Pojo 作为 POST 的参数

Jersey POST 方法正在接收空值作为参数