json - 用于消费 JSON 文件的 JAX-RS 生成错误 415
问题描述
我想创建一个带有 JAX-RS 和 Jersey 的 REST 服务,用于使用 JSON 结构。
我正在使用 org.glassfish.jersey 和 gradle,所以我插入了 gradle-build 文件:
compile group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet-core', version: '2.26-b03'
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-multipart', version: '2.26-b03'
compile group: 'org.glassfish.jersey.core', name: 'jersey-common', version: '2.26-b03'
compile group: 'org.glassfish.jersey.core', name: 'jersey-server', version: '2.26-b03'
compile group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.26-b03'
我在 web.xml 中激活了 POJO 映射功能:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
我编写了一个用于管理 JSON 实体的简单类,并使用了 XMLElement 注释。
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="polygongjson")
public class PolygonGJson {
@XmlElement(name="type")
String type;
@XmlElement(name="properties")
String properties;
@XmlElement(name="geometry")
String geometry;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getProperties() {
return properties;
}
public void setProperties(String properties) {
this.properties = properties;
}
public String getGeometry() {
return geometry;
}
public void setGeometry(String geometry) {
this.geometry = geometry;
}
@Override
public String toString() {
return "Track [type=" + type + ", properties=" + properties + ", geometry=" + geometry +"]";
}
}
最后,我创建了通过 POST 使用 JSON 结构的 REST 方法。
Path("/upload")
public class UploadRaster extends Application {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("/j_geojson/{id_user}")
public Response uploadGeoJSON(@PathParam("id_user") String id_user,
PolygonGJson polyIn){
try{
System.out.println("JSON: "+polyIn.toString());
return Response.status(200).entity(polyIn.getGeometry()).build();
}catch(Exception e){
return Response.status(500).entity(e.getMessage()).build();
}
}
}
我准备了一个简单的 json 文件:
{
"type": "Feature",
"properties": "test prop",
"geometry": "test geo"
}
为了测试我的方法,我以这种方式使用 curl:
curl -X POST -H "Content-Type: application/json" -d @./testgjsimple.json https://myserver:8443/mywarfile/api/upload/j_geojson/1
但服务器响应:
HTTP Status 415 - Unsupported Media Type
Unsupported Media Type
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
注意:我的应用服务器是 Tomcat 7。
任何想法?
解决方案
据我所知,XML 和 JSON 结构的处理方式不同。所以这部分:
...
@XmlRootElement(name="polygongjson")
public class PolygonGJson {
@XmlElement(name="type")
String type;
...
可能导致无法正确使用 JSON。
在没有这样的注释的情况下尝试它(它必须工作):
...
public class PolygonGJson {
String type;
...
或者如果您想调整一些特殊的东西,请使用 JSON 的注释(我更喜欢杰克逊)。
推荐阅读
- python - 如何连续异步播放不同的声音?
- xml - 查找并替换文档中的所有 GUIDS
- python-3.x - 如何计算百分比并识别包含未堆叠数据帧中的最大值的行和状态
- php - Add one more element to array in loop with PHP
- perl - 将所有 cwd 目录添加到数组中,逐个目录迭代目录并在其中仅打开保存 .xlsm 文件(Windows 10)
- symfony - symfony 上的无效凭据
- javascript - 理想地使用 Try/Catch
- reactjs - 使用 TypeScript 推送到状态数组 React
- node.js - 何时在 Express 中使用 return next(error) 与 return res.sendStatus
- excel - 有没有办法编写一个宏来循环文本连接excel中的多行数据?