JAX-RS(Jersey):JSON请求体中的字符串仍然转义

JAX-RS(Jersey):JSON请求体中的字符串仍然转义,jersey,glassfish,jax-rs,Jersey,Glassfish,Jax Rs,这让我快发疯了。我正在JAX-RS(Glassfish/Jersey)中开发一个REST端点。我有一个方法,应该接收一个字符串,存储它并返回它。整个端点应该使用并生成JSON。但每次我向该方法发布字符串时,它都以转义形式传递给我。例如,如果我发布: fetch("http://localhost:8080/myapp/rest/myresource", { method: "post", credentials: 'same-origin', body: JSON.stringify

这让我快发疯了。我正在JAX-RS(Glassfish/Jersey)中开发一个REST端点。我有一个方法,应该接收一个字符串,存储它并返回它。整个端点应该使用并生成JSON。但每次我向该方法发布字符串时,它都以转义形式传递给我。例如,如果我发布:

fetch("http://localhost:8080/myapp/rest/myresource", {
  method: "post",
  credentials: 'same-origin',
  body: JSON.stringify("test\ntest"),
  headers: {
    "Content-Type": "application/json"
  }
})
资源是:

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyResource {

    @POST
    public String set(@NotNull String value){
        // store it
        return storedValue;
    }
}
然后存储并返回到客户端的内容是:

"test\ntest"
但是,如果我将字符串包装在对象中:

fetch("http://localhost:8080/myapp/rest/myresource", {
  method: "post",
  credentials: 'same-origin',
  body: JSON.stringify({value: "test\ntest"}),
  headers: {
    "Content-Type": "application/json"
  }
})
有资源

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyResource {

    @XmlRootElement
    public static class Wrapper {
        public String value;
    }

    @POST
    public String set(@NotNull Wrapper wrapper) {
        String value = wrapper.value;
        // store it
        return storedValue;
    }
}
然后存储并返回给客户机的值是

test
test
我错过什么了吗

Glassfish 4.1.1
 - jersey 2.10.4-0
 - json 1.0-0.1

这似乎是一个特性:当传递字符串时


我将把这个留给后代。

为了使用JAX-RS将原始字符串发布为JSON,您可以使用fasterxml.jackson提供的“@JsonRawValue”

Class ResponseObject {

@JsonRawValue
String jsonString;
...
}
无论何时发送响应对象,都会发送不带斜杠的unscaped json