【问题标题】:Spring MVC POST methodSpring MVC POST 方法
【发布时间】:2014-06-27 08:38:37
【问题描述】:

这是我的控制器类 sn-p

@RequestMapping(value = "/createAgent", method = RequestMethod.POST)
@ResponseBody
public String createAgent(@RequestBody String json) {
    System.out.println(json);
} 

如何将 JSON 对象传递给此方法。我可以以任何方式通过 url 传递它吗?如果我这样通过(虽然这看起来很不对),

http://localhost:8080/SurveyApp3/createAgent/{"a_id": 8746574632, "pwd": "abcd", "pim_id": 3, "m_id":9738247882} 

我收到一个错误提示

WARNING: No mapping found for HTTP request with URI [/SurveyApp3/createAgent/%7B%22a_id%22:%208746574632,%20%22pwd%22:%20%22abcd%22,%20%22pim_id%22:%203,%20%22m_id%22:9738247882%7D] in DispatcherServlet with name 'spring'

我还尝试使用欢迎文件 index.jsp 中的 ajax 请求

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


 <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
 <script type="text/javascript" >

 function sendAjax() {

 $.ajax({
     url: "/createAgent",
     type: 'POST',
     dataType: 'json',
     data: "{\"a_id\":7645534265,\"pwd\":\"abcd\", \"pim_id\":3, \"m_id\":9738247882}",
     contentType: 'application/json',
     success: function(data) {
         alert(data.a_id + " " + data.pwd);
    },
    error:function(data,status,er) {
       alert("error: "+data+" status: "+status+" er:"+er);
    }
 });
}
</script>
</body>
</html>

当我转到这个网址时,

http://localhost:8080/SurveyApp3/createAgent

我收到一条错误消息,指出不支持 GET 方法。我还是这个话题的新手,我无法弄清楚如何将 JSON 对象作为参数发送到 post 方法。 我哪里错了?

谢谢!

【问题讨论】:

  • 当您在浏览器中访问 URL(如 localhost:8080/SurveyApp3/createAgent)时,默认方法是 GET。由于您的方法是 POST,服务器返回 405 Not Supported。
  • 我如何将 JSON 值传递给方法呢?我不完全知道 (@RequestBody String json) 是什么以及如何获得它的价值。
  • 运行 AJAX 调用时会发生什么?
  • 我收到此错误警告:不支持请求方法“GET”
  • 你试过 $.post 吗? api.jquery.com/jquery.post

标签: java json spring-mvc http-post


【解决方案1】:

在 Spring Controller 中,每个方法都必须返回一个视图名称。在您缺少的代码中,这就是您得到的原因 警告:找不到带有 URI 的 HTTP 请求的映射错误,请将
return "viewname" 添加到您的代码中。
此外,使用 @ModelAttribute --- 对象
@PathVariable --- 用于字符串
@RequestParam --- 用于字符串数组。

希望这会奏效...

【讨论】:

  • 不就是为了在网页上渲染一些响应吗?无论如何,我尝试返回字符串(返回 json;)但没有工作。
【解决方案2】:

在您的控制器中保留此代码:

@RequestMapping(value = "/createAgent", method = RequestMethod.POST)
public @ResponseBody String createAgent(@RequestParam String jsonText) {
    System.out.println(jsonText);
} 

在你的 Ajax 调用中保留这个:

$.ajax({
     url: "/createAgent",
     type: 'POST',
     dataType: 'json',
     data: {
     "jsonText":"{'a_id':7645534265,'pwd':'abcd', 'pim_id':3,'m_id':9738247882}"
            },
     success: function(data) {
         alert(data.a_id + " " + data.pwd);
    },
    error:function(data,status,er) {
       alert("error: "+data+" status: "+status+" er:"+er);
    }
 });

希望对您有所帮助。 :)

【讨论】:

  • 说警告:不支持请求方法“GET”。是否需要在 RequestMethod 中也添加 Get 方法?
【解决方案3】:
  • 首先尝试检查下一个依赖项:

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
    
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
    
  • 您是否使用按钮发送帖子 URL,是吗?你有没有配置它来调用“sendAjax()”方法?

  • 您可以检查在 Ajax 中键入的 url(检查行的正确语法:

网址:“/createAgent”

也许你用“/”符号打错了

  • 最后还有一个 Json 转换器。您是否在 bean 配置中定义了它?

【讨论】:

  • 我没有使用 maven。但我确实添加了杰克逊罐。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-28
  • 2019-02-02
  • 1970-01-01
  • 2020-08-12
  • 2018-10-05
  • 2018-04-01
相关资源
最近更新 更多