使用$ .post从JQuery调用POST方法时不起作用

时间:2017-02-20 03:25:32

标签: java jquery spring rest post

我不确定为什么我的帖子方法不起作用。我想我也正确地设置了它。我有一个接受POST方法的Rest Controller。以下是我使用的代码。任何帮助表示赞赏。

对于JS部分:

$.post("http://localhost:8080/WebApplicationService/service/registerUser", registrationDetails, function(data) {

    });

对于服务器部分:

 @RequestMapping(value = "registerUser", method = RequestMethod.POST)
    public ResponseEntity<Object> registerUser(@RequestBody final RegistrationBean pBean)
            throws Throwable {

        System.out.println("hello");

        try {
//          this.logic.register(pBean);
        } catch (final Throwable e) {
            System.out.println("Error!");
            e.printStackTrace();
        }

        return new ResponseEntity<Object>("Hello", HttpStatus.OK);
    }

更多信息: 我已经检查了registrationDetails对象和RegistrationBean,它们都具有相同的字段和相同的类型。我还用@Component注释了RegistrationBean。包含registerUser的控制器映射到&#34; / service&#34;。

我得到的错误是POST http://localhost:8080/WebApplicationService/service/registerUser 415 ()

我还能错过什么?

1 个答案:

答案 0 :(得分:0)

我发现了我所缺少的东西。显然,我在我的pom.xml中缺少依赖项,我认为Jackson需要将JSON字符串转换为POJO对象。基本上,我在我的pom.xml中添加了Jackson核心和Jackson databind,它解决了这个问题。 以下依赖项添加到pom.xml:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.6</version>
    </dependency>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>4.1.1.4</version>
    </dependency>
相关问题