RESTEasy服务返回不带双引号的JSON字符串

RESTEasy服务返回不带双引号的JSON字符串

本文介绍了JAX-RS + RESTEasy服务返回不带双引号的JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JAX-RS + RESTEasy的新手

I'm new to JAX-RS + RESTEasy

阻碍我的是该服务返回了不带双引号的JSON字符串.

What is impeding me is that the service return the JSON String without double quotes.

@Path("/hello")
public class HelloService {

    @GET
    @Path("say")
    @Produces(MediaType.APPLICATION_JSON)
    public String say() {
        return "Hello";
    }
}

当我呼叫"/hello/say"时,它只会返回 Hello ,但我期望的是"Hello"

When I call "/hello/say" it just returns Hello but what I'm expecting is "Hello"

用Google搜索几天.我有一个使用JQuery的Javascript,它像这样调用服务:

Googled for several days. I have a piece of Javascript using JQuery which calls the service like this:

$(function(){
    $.ajax({
            url : "services/hello/say",
            context : $('#message'),
            success : function(data){
                    $(this).html(data);
                },
            error : function(xhr, ajaxOptions, thrownError){
                    $(this).html(xhr.status + "<br>" + thrownError);
                }
        });
    });

这就是结果

SyntaxError: Unable to parse JSON string

尽管状态为200.有办法解决此问题,而不是手动在字符串中添加双引号吗?

Although the status is 200.Is there a way to solve this rather than manually adding the double quotes to the string ?

推荐答案

您将哪个JSON实现与RESTeasy,Jettison或Jackson一起使用?

Which JSON implementation are you using together with RESTeasy, Jettison or Jackson?

只是和杰蒂森一起尝试过,同样的问题.我只是研究了JSON规范,然后从那里开始编写,值本身不是有效的JSON响应(请参见 http://www.json.org/).您有一个对象或一组值.

Just tried with Jettison, same problem. I just looked into the JSON specification and from there it is written, that a value itself is not a valid JSON response (see http://www.json.org/). Either you have an object or an array of values.

我想这可能是问题所在,因为RESTeasy不确定由于不返回单一类型的对象(例如String,Integer等)而不会返回对象.

I guess that could be the problem as RESTeasy isn't sure what to do as you don't return an Object moreover a single type (e.g. String, Integer, ...).

想知道如果返回一个数组会发生什么情况.

Wonder what happen if one returns an array.

这篇关于JAX-RS + RESTEasy服务返回不带双引号的JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 07:02