java - 使用 JSON 作为 application/x-www-form-urlencoded

标签 java json rest jax-rs

我有一个 JAX-RS 项目,其中 POST 不起作用。我有 @GET URL,工作正常。除了这个 @POST 之外,一切似乎都工作正常。

@POST
@Path("/json/insert")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/x-www-form-urlencoded")
public String postJSONInsert(
    @FormParam("instance") String instance,
    @FormParam("db") String table) {

    String json;
    EDPObject edp_obj = new EDPObject();

    try {

        json = edp_obj.insert("json", instance, table);

    } catch(Exception e) {

        edp_obj.endSession();
        json = handleJSONError(e);

    }

    return json;

}

在客户端尝试此操作时,在 Firebug 中获取 500 尚未连接:

$.ajax('http://127.0.0.1:8070/sixaxis/webapi/json/insert', {
    data: {
        db: '17:2',
        instance: 'shawn'
    },
    dataType: 'json',
    type: 'POST'
});

最佳答案

你尝试过吗:

@Consumes(MediaType.APPLICATION_JSON)

因为你的 jQuery 是:

dataType:'json'

更新(感谢反馈): 那么方法至少应该是:

@POST
@Path("/json/insert")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String postJSONInsert( Map<String,Object> params ){
   // Your business logic
}

关于java - 使用 JSON 作为 application/x-www-form-urlencoded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19645577/

相关文章:

java - Comparable Generic 如何使用 ist

python - 使用没有值的列从 csv 创建嵌套 JSON 的问题

c# - 将字符串返回到自定义对象类型

php - WP $post 对象可以从主题文件输出为 REST-API 格式的 JSON 吗?

java - Camel Rest DSL 删除另一条额外路线

java - Grails 域类从哪里获取它们的查询方法?

java - 设计: where to put Manager options?

java - Java多态是如何做到的

json - 无法从链接解析 JSON - 没有错误,但该函数在尝试访问 URL 后返回

PHP 调用另一个 PHP 页面进行 MySQL 查询(返回 JSON 数据)