Java Jersey JSON 服务不 return 引用字符串?

Java Jersey JSON service doesn't return quoted string?

这是一个使用 Jersey 编写的简单 Web 服务

@GET
@Produces(MediaType.APPLICATION_JSON)
public Object interpretationJson() {                        
    String o = "a simple string";       
    return o;
}

对此的回应是:

HTTP/1.1 200
Content-Type: application/json
Content-Length: 15
Date: Mon, 02 Oct 2017 23:18:14 GMT

a simple string

响应正文中的字符串不应该被引用吗?我不认为这是有效的 JSON。响应不应该是:

HTTP/1.1 200
Content-Type: application/json
Content-Length: 15
Date: Mon, 02 Oct 2017 23:18:14 GMT

"a simple string"

这让我有点困惑。本来以为是客户端的问题,现在觉得问题是 Jersey 返回的字符串无效 JSON - C# Parsing json that may have simple types

这里引用了 Jackson 的开发者 (Tatu Saloranta / cowtowncoder) 的话,说明了为什么要这样处理一个简单的字符串。

This is not the reason String is declared untouchable. Rather, choice is between:

Write input String as JSON String, i.e. surround it with double-quotes and escape necessary characters, or

Write String exactly as-is, assuming user wanted to produce exactly that output (presumable hand-encoded JSON).

Since there is no metadata to tell what is user's intention, Jackson is being conservative and using latter choice. This is also prudent considering that JSON Specification only considers JSON Objects and JSON Arrays as valid JSON content -- so strictly speaking, returning a JSON String would produce invalid JSON anyway.

Referenced issue...

"Untouchable" 是 Jackson 对将 return 完全按原样编辑的类型的术语。

如果需要,您可以轻松 return 带引号的字符串,并且有多种方法可以做到这一点。但我想那不是你的问题。

public class TestGetterSetter{

private String name ;

public void setName(String name){
    this.name = name ;
}


public String getName(String name){
    return this.name ;
}}

Using this POJO class Set the value and return this POJO Class Object