当前位置: 首页 > 知识库问答 >
问题:

java spring rest参数不起作用

谢誉
2023-03-14

我创建了将数据传递给spring rest的代码,但这些代码不能正常工作请参见下面的代码:

@PutMapping("/notification/save/{id}")
public @ResponseBody String update(@PathVariable("id") long id, @RequestBody  AccountNotification accountNotification){
    String result="ok";
    ServiceAccount serviceAccount =  new ServiceAccount();
    serviceAccount.setId(id);
    //accountNotification.setServiceAccount( serviceAccount );
    //result =notificationsService.update(id,accountNotification);

    JSONObject jObject = new JSONObject();
    try
    {
        JSONArray jArray = new JSONArray();
             JSONObject accountNotificationJSON = new JSONObject();

             if(result.equals("ok")) {
                 accountNotificationJSON.put("success", "true");
                 accountNotificationJSON.put("messages", "Successfully Saved");
             }
             else {
                 accountNotificationJSON.put("success", "false");
                 accountNotificationJSON.put("messages", "NOT Successfully Saved");

             }
             jArray.put(accountNotificationJSON);

        jObject.put("data", jArray);
    } catch (JSONException jse) {
        logger.error( jse.getMessage());
    }


    return jObject.toString();
}

我的javascrit有:

$("#saveChanges").on('click',function(){
     var params1={
             proxy:$(proxy).val() ,
             port:$(port).val(),
             uri:$(uri).val(),
             actived:$(actived).val()=="on"?true:false
     };

     $.ajax({
         url: 'notification/save/'+id,
         params: params1,
         type: 'post',
         success:function(response) {
             if(response.data[0].success == "true") {                      
                 $(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
                      '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                      '<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.data[0].messages+
                     '</div>');


                 // close the modal
                 $("#editMember").modal('hide');

             } else {
                 $(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
                      '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                      '<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.data[0].messages+
                     '</div>');
             }
         },
         error: function(x, e) {
                alert(x);
              }

     });

共有1个答案

葛书
2023-03-14

在您的Rest代码中有一个put映射@putmapping(“/notification/save/{id}”),但是在post方法调用的js中类型:'post',so它返回400bad request,您应该使用type:put,它等于您的Rest方法。

 @PutMapping("/notification/save/{id}")
 public @ResponseBody String update(@PathVariable("id") long id, 
 @RequestParam  AccountNotification accountNotification){
 .....
 }



  $.ajax({
     url: 'notification/save/'+id,
     params: params1,
     type: 'put',
     success:function(){
 }})
 类似资料:
  • 问题内容: 有谁知道为什么这不起作用? 这确实有效: 问题答案: 调用中的第二个参数是一个配置对象。您想要这样的东西: 有关更多详细信息,请参见http://docs.angularjs.org/api/ng.$http的“ 参数” 部分。

  • 我正在尝试使用Chrome扩展Postman测试一个简单的PHP页面。当我发送URL参数时,脚本运行良好(例如变量在参数中可用)。当我将它们作为参数发送时,参数仅包含。 剧本: 我错过了什么?

  • 问题内容: 我正在尝试在Volley JsonObjectRequest中发送POST参数。最初, 它 通过遵循官方代码所说的在JsonObjectRequest的构造函数中传递包含参数的JSONObject来为我 工作 。然后一下子它停止工作了,我没有对以前工作的代码进行任何更改。服务器不再识别任何正在发送的POST参数。这是我的代码: 这是服务器上的简单测试器PHP代码: 我得到的答复是 昨天

  • 问题内容: 我有一个带有两列(名称,职业)的表。我想以这种格式输出值。 听到第一个是名称,方括号中的值是其职业的第一个字母。 到目前为止,我所做的是 这样的输出值 获得所需的格式,我尝试了这个 那么它就会抛出类似这样的错误。 SELECT CONCAT(Name,’(’,SUBSTR(Occupation,1,1),’)’)FROM OCCUPATIONS *错误在第1行:ORA-00909:无效

  • 我正在使用 swagger(1.2) 来记录我的球衣 jaxrs (2.1) api。我使用 swagger-ui 来显示我的 api。对于 http GET 请求,我在 api endpoint中有一个查询参数作为 @QueryParam(“token”)。Swagger 正在显示此查询参数的字段,但是当我每次收到它的空值时单击“尝试一下!”按钮时。 我还用@ApiParam注释了@ query

  • 我们在应用程序中使用和。我们的项目通过以下方式创建了RxJava订阅服务器: null 但是,如果不使用参数Subscriber,同时创建一个全新的Subscriber,那么Observable就可以正常工作。 你知道我怎样才能让这件事和参数订阅者一起工作吗?我们希望从活动中调用无法从network service类访问的UI方法onComplete。 这是我的活动代码: 但是这个版本的网络服务类