5

我目前有一个使用CXF公开的RESTFul Web 服务。该方法看起来像这样。

@GET
@Path("/getProfile/{memberno}/{userid}/{channelid}")
@Consumes("application/xml")
public MaintainCustomerProductResponse getUserDetails(
        @PathParam("memberno") String membernumber,
        @PathParam("userid") String userid,
        @PathParam("channelid") String channelid){
//DO Some Logic here.
}

这可以通过以下 URL 访问,并在提交有效数据后得到响应。

http://server.com:8080/UserService/getProfile/{memberno}/{userid}/{channelid}

问题:如何传递空值userid

如果我简单地忽略该{userId}请求不会在服务器端 Web 服务上命中。

例子

http://server.com:8080/UserService/getProfile/1001//1

注意:我正在使用SOAPUi测试并且没有给出userId值我看到以下响应SOAPUI并且请求没有命中服务器。

SOAPUI 响应

<data contentType="null" contentLength="0"><![CDATA[]]></data>
4

6 回答 6

7

变量的默认匹配是[^/]+?(至少一个字符)您可以手动将匹配设置为[^/]*?,它也应该匹配空字符串。

只需将格式添加到 userid 变量:

@Path("/getProfile/{memberno}/{userid: [^/]*?}/{channelid}")
于 2012-12-21T12:12:17.960 回答
2

请进行更改@Path("/getProfile/{memberno}/{userid}/{channelid}") @Path("/getProfile/{memberno}/{userid = default}/{channelid}"),使其接受用户 id 的空值。您可以使用 Uri 模板为可选参数指定默认值。请参阅

http://msdn.microsoft.com/en-us/library/bb675245.aspx

于 2013-10-09T05:50:00.233 回答
0

我通过解决方法解决了这个问题。我创建了另一个只接受两个参数的方法。现在基于请求 URL 调用不同的方法

于 2012-12-31T10:23:58.710 回答
0
@GET
@Path("qwer/{z}&{x:.*?}&{c}")
@Produces(MediaType.TEXT_PLAIN)
public String withNullableX(
        @PathParam("z") Integer z,
        @PathParam("x") Integer x,
        @PathParam("c") Integer c
) {
    return z + "" + x + "" + c;
}

当您使用
http://localhost:8080/asdf/qwer/1&&3
调用时, 您将看到:
1null3

于 2018-08-15T12:23:12.143 回答
0

需要更详细地描述所有可能的情况。查看基于 Spring Framework 的示例:

        @RestController
        @RequestMapping("/misc")
        public class MiscRestController {
    ...
            @RequestMapping(value = { "/getevidencecounts/{userId}",
                    "/getevidencecounts/{userId}/{utvarVSId = default}" }, method = RequestMethod.GET)
            public ResponseEntity<EvidenceDokumentuCountTO> getEvidenceCounts(
                    @PathVariable(value = "userId", required = true) Long userId,
                    @PathVariable(value = "utvarVSId", required = false) Long utvarVSId) {
...

我们需要使用像.../misc/1(1)、.../misc/1/25(2)、.../misc/1/null(3) 这样的路径。路径 1 表示/getevidencecounts/{userId}。路径 2 和 3 /getevidencecounts/{userId}/{utvarVSId = default}。如果您确定默认值为 null,您也可以= default= null值替换声明。

于 2020-01-10T15:11:11.293 回答
0

您可以使用 URL 编码,通过%00. 参考这个

例子:http://server.com:8080/UserService/getProfile/1001/%00/1

于 2019-01-10T20:22:42.410 回答