REST不返回备用字符。有些字符丢失了

时间:2015-03-25 09:45:02

标签: java json rest

我正在尝试从rest客户端获取一个字符串作为输入,并使用备用字符返回json。但是,返回时字符串中缺少一些字符。在控制台上,所有字符都被打印出来。

@Path("/person")
public class PersonResource {

    @GET
    @Produces("application/json")

    @Path("/{userid}")
    public Response getJson(@PathParam("userid") String userId) {

        return Response.ok(test(userId)).build();
    }

    public String test(String userId) {

        if (userId.length() == 0) {
            System.out.println("Enter User name");
        }
        System.out.println(userId);
        char[] c = userId.toCharArray();

        userId = "";
        for (int i = 0; i < c.length; i=i+2) {
            System.out.println(Character.toString(c[i]) + " "  + i );
            if ((int) c[i] > 127) {
                return "invalid chars";
            } else if (c[i] % 2 == 0) {
                userId = userId + Character.toString(c[i]);
            }
        }

来自REST客户端的请求是

http://localhost:8084/JSONProjectTest/api/person/HelloWorld

REST客户端返回Hll.json

在控制台上,显示以下内容:

HelloWorld
H 0
l 2
o 4
o 6
l 8

我尝试更改字符的小数,但没有出现。

1 个答案:

答案 0 :(得分:3)

您不仅要跳过替代字符 - 您还要跳过UTF-16代码单元不均匀的字符:

if (c[i] % 2 == 0)

排除&#39; o&#39; (U + 006F)两次,这就是为什么你得到&#34; Hll&#34;而不是&#34; Hlool&#34;。

不清楚为什么你完全得到if声明,但看起来你不应该拥有它。 (我也删除了对Character.toString()的所有调用并使用StringBuilder而不是重复的字符串连接,但这是另一回事。)

相关问题