我是Jersey的新用户,并尝试根据Jackson使用these instructions进行自动JSON处理。
我@GET
和@POST
与application/json
合作,但我希望使用text/plain
等其他mime类型进行POST,主要是curl
application/x-www-form-urlencoded
1}}工作时没有指定标题(我认为可能需要处理text/plain
,但对于这个问题,我现在仍然坚持使用package hellotest;
// omit imports for space
@Path("/hello")
public class Hello {
@GET
@Produces(MediaType.APPLICATION_JSON)
public HelloJson sayHello() {
return new HelloJson("Hello there!", "buttface");
}
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
@Produces(MediaType.APPLICATION_JSON)
public HelloJson sayHelloPost(HelloJson h) {
return new HelloJson(h.getBar() + "p", h.getWhee() + "p");
}
}
)。
资源类是:
POST
服务器宣传text/plain
curl -X OPTIONS http://localhost:8080/auth/rest/hello
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.23.2 2016-08-08 17:14:55"/>
<grammars/>
<resources base="http://localhost:8080/auth/rest/">
<resource path="hello">
<method id="sayHelloPost" name="POST">
<request>
<representation mediaType="application/json"/>
--> <representation mediaType="text/plain"/> <--
</request>
<response>
<representation mediaType="application/json"/>
</response>
</method>
<method id="sayHello" name="GET">
<response>
<representation mediaType="application/json"/>
</response>
</method>
</resource>
</resources>
</application>
即可:
text/plain
但是,尝试发布curl -X POST -H "Content-Type: application/json"
-d '{"bar": "somebar", "whee": "somewhee"}'
http://localhost:8080/auth/rest/hello
{"bar":"somebarp","whee":"somewheep"}
curl -X POST -H "Content-Type: text/plain"
-d '{"bar": "somebar", "whee": "somewhee"}'
http://localhost:8080/auth/rest/hello
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 415 Unsupported Media Type</title>
</head>
<body><h2>HTTP ERROR 415</h2>
<p>Problem accessing /auth/rest/hello. Reason: <pre>
Unsupported Media Type</pre></p><hr><a *snip rest of line*
</body>
</html>
失败(为了清晰起见,添加了换行符):
[[abc]]
我做错了什么?