Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jersey Web服务:使用JSON请求_Java_Angularjs_Json_Rest_Jersey - Fatal编程技术网

Java Jersey Web服务:使用JSON请求

Java Jersey Web服务:使用JSON请求,java,angularjs,json,rest,jersey,Java,Angularjs,Json,Rest,Jersey,我有一个由WitjJava和Jersey制作的web服务。我想接收一个JSON请求,并解析JSON以保存存储在数据库JSON上的值 这是mi web服务代码: @Path("companies") public class Companies { @Path("add") @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public JSO

我有一个由WitjJava和Jersey制作的web服务。我想接收一个JSON请求,并解析JSON以保存存储在数据库JSON上的值

这是mi web服务代码:

@Path("companies")
public class Companies {

    @Path("add")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject addCompanies(JSONObject inputJsonObj){

        String input = (String) inputJsonObj.get("company");
        String output = "The input you sent is :" + input;
        JSONObject outputJsonObj = new JSONObject();
        outputJsonObj.put("output", output);

        return outputJsonObj;
    }
}
客户端由AngularJS制成:

$scope.company = "";
    $scope.submit = function(){
        // Writing it to the server
        //      
        var dataObj = {
                company : $scope.company
        };  
        var res = $http.post('http://localhost:8080/WS-Test2/crunchify/companies/add', dataObj);
        res.success(function(data, status, headers, config) {
            $scope.message = data;
            notify("succes");
        });
        res.error(function(data, status, headers, config) {
            //alert( "failure message: " + JSON.stringify({data: data}));
            notify("fail");
        });
    };
这是我将JSON传递给web服务时遇到的错误:

Status Code:415
这是我发出的请求:

{"company":"Testing2"}
这是我的网络选项卡:


没有进一步的配置,
JSONObject
不是Jersey支持的东西。您只需要使用字符串

public String addCompanies(String json){
    JSONObject inputJsonObj = new JSONObject(json)

    String input = (String) inputJsonObj.get("company");
    String output = "The input you sent is :" + input;
    JSONObject outputJsonObj = new JSONObject();
    outputJsonObj.put("output", output);

    return outputJsonObj.toString();
}
如果您真的想使用
JSONObject
,您可以签出

另请参见:

  • 了解Jersey如何处理序列化和反序列化

mm它看起来很奇怪。。我已经做了一些关于错误代码415的研究。它看起来不支持该格式。那么。您确定如何设置后端api的json类型吗?也许是像@Consumes({MediaType.APPLICATION\u JSON,MediaType.APPLICATION\u JSON})这样的东西,我尝试了@Consumes({MediaType.APPLICATION\u JSON,MediaType.APPLICATION\u JSON}),但是我得到了同样的错误MM看起来很奇怪,因为$http.post send用于默认JSON。。。因此,我可以猜问题出在后端配置中..但我不懂java:-)转到浏览器的开发人员工具网络选项卡并单击相应的服务调用。截图并更新您的问题。@Aravind I postedYes正在工作,我将检查JAX-RS实体提供程序post以实现它。谢谢