通过发送和接收 json 数据获取路径变量 Rest spring mvc

get pathvariable with sending and recieving json data Rest spring mvc

我正在尝试使用 spring mvc 学习 Rest Web 服务。我已关注 this link.

我能够接收和发送 Json 数据,但我无法通过调用传递路径变量。我无法理解这个问题。

这是我的控制器:

package com.akash.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/myRest")
public class MyRestController {

    @RequestMapping(value = "/myAction1/{param1}/", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody List<Abc> myAction1(@PathVariable String param1) {

        Abc ob = new Abc();
        ob.setAge(20);
        ob.setName("obama-"+param1);
        List<Abc> listOfAbc = new ArrayList<Abc>();
        listOfAbc.add(ob);
        return listOfAbc;
    }

    /*
     * curl -X GET -H "Accept:application/json"
     * http://localhost:8080/myRest/myAction1/123
     */

    @RequestMapping(value = "/myAction2", method = RequestMethod.POST, headers = {
            "Accept=application/json", "Content-type=application/json" })
    public @ResponseBody List<Abc> myAction2(@RequestBody AbcWrapper wrapper) {

        Abc ob = new Abc();
        ob.setAge(wrapper.getListOfAbc().size());
        ob.setName("obama");
        List<Abc> listOfAbc = new ArrayList<Abc>();
        listOfAbc.add(ob);
        return listOfAbc;
    }

    /*
     * curl -X POST -H "Accept: application/json" -H
     * "Content-Type: application/json" http://localhost:8080/myRest/myAction2
     * -d "{\"listOfAbc\":[{\"age\":20,\"name\":\"akash\"}]}"
     */
}

class Abc {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

class AbcWrapper {
    List<Abc> listOfAbc;

    public List<Abc> getListOfAbc() {
        return listOfAbc;
    }

    public void setListOfAbc(List<Abc> listOfAbc) {
        this.listOfAbc = listOfAbc;
    }
}

我可以执行myAction2,但无法执行myAction1。 如果我从 myAction1 中删除 pathvariable 一切正常。

请帮忙。

我找到了解决问题的方法。错误很多。

(1)第一个问题是 spring 安全性,它正在验证 url 并将其重定向到登录页面。

我改了这个

<http pattern="/myRest/*" security="none" />

<http pattern="/myRest/**" security="none" />

(2)将 myAction1 方法更改为

@RequestMapping(value = "/myAction1/{param1}", method = RequestMethod.GET, headers = "Accept=application/json")
    public @ResponseBody List<Abc> myAction1(@PathVariable("param1") String param1, Model model) {

在 {param1}

之后删除了一个额外的斜线

我从 c​​url -i 参数

获得的所有信息

解题前

HTTP/1.1 302 Found
Date: Sat, 07 Feb 2015 05:35:55 GMT
Set-Cookie: JSESSIONID=1kk70yuy7kc501glpcd0m9t4py;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: http://localhost:8080/login;jsessionid=1kk70yuy7kc501glpcd0m9t4py
Content-Length: 0
Server: Jetty(9.2.3.v20140905)

解决问题后

HTTP/1.1 200 OK
Date: Sat, 07 Feb 2015 05:37:55 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(9.2.3.v20140905)

[{"age":20,"name":"obama-123"}]

干杯