java 使用 JAX-RS 使用 JSON 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38125756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consume JSON string with JAX-RS
提问by Dennis
Due to RFC 7159, a string can be a valid JSON document:
由于RFC 7159,字符串可以是有效的 JSON 文档:
JSON-text = ws value ws
...
value = false / null / true / object / array / number / string
...
string = quotation-mark *char quotation-mark
Thus, a valid JSON can be "Hello World"
.
因此,有效的 JSON 可以是"Hello World"
.
Is it possible consume such a a JSON string with JAX-RS?
是否可以使用 JAX-RS 使用这样的 JSON 字符串?
回答by Wilson
JAX-RS has a @Consumes
annotation to specify the MIME media types of representations a resource can consume that were sent by the client.
JAX-RS 有一个@Consumes
注解来指定客户端发送的资源可以使用的 MIME 媒体类型的表示。
Following is an example to accept a request with a application/json
media type:
以下是接受具有application/json
媒体类型的请求的示例:
@Path("/foo")
public class FooResource {
@POST
@Consumes("application/json")
public Response bar(InputStream entity) {
return Response.ok.build();
}
}
If you send a request with a header of Content-Type: application/json
, a response with a 200 OK
status will return to you. But if you send a request with a header of Content-Type: application/xml
, a response with 406 Not Acceptable
will return. It is because @Consumes
specify that request with JSON is acceptable but not others.
如果您发送标头为 的请求,则将返回Content-Type: application/json
带有200 OK
状态的响应。但是,如果您发送标头为 的请求Content-Type: application/xml
,406 Not Acceptable
则会返回一个响应。这是因为@Consumes
指定使用 JSON 的请求是可以接受的,而不是其他的。
You can observe that @Consumes
is noting about how to parse a request entity, but specify what media type should be accepted.
您可以观察到这@Consumes
不是关于如何解析请求实体,而是指定应接受的媒体类型。
You can see that the above example has a parameter of InputStream entity
. It is in fact the request entity. You can parse in the resource method of bar
with a JSON parser library, e.g. Hymanson:
可以看到上面的例子有一个参数InputStream entity
。它实际上是请求实体。您可以bar
使用 JSON 解析器库解析资源方法,例如Hymanson:
@POST
@Consumes("application/json")
public Response bar(InputStream entity) {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.readValue(inputStream, String.class);
System.out.println(json);
return Response.ok.build();
}
If you send a request with a body of "Hello World", you will see "Hello World" in your console. This is because Hymanson
know how to parse a JSON document specified in RFC 7159, but not JAX-RS.
如果您发送带有“Hello World”正文的请求,您将在控制台中看到“Hello World”。这是因为Hymanson
知道如何解析 RFC 7159 中指定的 JSON 文档,而不是 JAX-RS。
In fact, JAX-RS implementation like RESTEasy already have JSON support with popular JSON parser library. Therefore you can simply create a resource method as follows:
事实上,像 RESTEasy 这样的 JAX-RS 实现已经通过流行的 JSON 解析器库支持 JSON。因此,您可以简单地创建一个资源方法,如下所示:
@POST
@Consumes("application/json")
public Response bar(String json) {
System.out.println(json);
return Response.ok.build();
}
You should get the same result.
你应该得到相同的结果。
Behind the scenes, JAX-RS has MessageBodyReaderto convert HTTP request body to Java object. It is the class provide mapping services from representation to a corresponding Java type. For example, Hymanson-jaxrs-providersimplements JAX-RS MessageBodyReader
and MessageBodyWriter
handlers for JSON data formats.
在幕后,JAX-RS 使用MessageBodyReader将 HTTP 请求正文转换为 Java 对象。它是提供从表示到相应 Java 类型的映射服务的类。例如,Hymanson-jaxrs-providers为 JSON 数据格式实现 JAX-RSMessageBodyReader
和MessageBodyWriter
处理程序。
For more details about JAX-RS, you can have a look to JSR-000339 JAX-RS 2.0 Specification
有关 JAX-RS 的更多详细信息,您可以查看JSR-000339 JAX-RS 2.0 规范
回答by cassiomolin
Yes, it's possible:
是的,这是可能的:
@Path("foo")
public class Foo {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response biz(String json) {
return Response.ok(json).build();
}
}
And your request would be like:
你的要求是这样的:
curl -X POST --header 'Content-Type: application/json' \
--header 'Accept: application/json' \
-d '"Hello World"' 'http://localhost:8080/api/foo'
回答by djhr
Yes, it's possible.
是的,这是可能的。
On JAX-RS define your method as:
在 JAX-RS 上将您的方法定义为:
@POST
@Consumes(MediaType.APPLICATION_JSON)
public void example(String s) { ... }
On client side, send the payload without encoding to json, e.g:
在客户端,将没有编码的有效负载发送到 json,例如:
var payload = "helloworld"; // OK
...
Rather than:
而不是:
var payload = JSON.stringify("helloworld"); // KO
...