How to Pass Json Object as a Pathvariable to Spring Controller

Passing a JSONObject as a parameter on Spring MVC

You're confusing GET and POST methods.

Either:

  1. Use POST method and actually post the JSON contents to the controller. You can debug this using any REST client, eg. Advanced Rest Client for Chrome.
  2. Use GET method (as you are currently). But you have to pass the JSON value as an actual parameter called jsonparam. So, your example should change to:

     http://localhost:8080/v1/wsp/url/lookup/jsonparam=%7B%22max_hops%22%3A3%2C%22url%22%3A%22http%3A%2F%2Fbubba.com%2Ffoo%2Fbar%22%7D

The latter is less common.


Edit:

On second look at your URL, I suspect you're confusing two Spring annotations:

  1. @PathVariable("jsonparam") and:
  2. @RequestParam("jsonparam")

You're using @RequestParam while your URL indicates need for @PathVariable.

Edit2:

However, as can be read here: http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates the path variable can be passed a value of any simple type. I believe JSON isn't one of them, hence your problems.

I would strongly recommend using POST for interchanging JSON values. However, if that is not an option, I would recommend sticking with GET method, @RequestParam for accessing parameter value, and passing the JSON value like in the corrected example above.

Sending Json Data to Spring Controller

You can't use List as PathVariable. Make it RequestBody instead and fix your JS code to send $scope.list as a body, not append it to url:

-$http.post("/app/orderDetails/saveOrder/"+$scope.list)
+$http.post("/app/orderDetails/saveOrder", $scope.list);

Also create a wrapper object with id and quantity fields, something like:

public class CartItem {
private Long id;
private Long quantity;
//getters and setters
}

Then you'll be able to parameterize your List with this object:

- public @ResponseBody Object saveOrder(@PathVariable List list){
+ public @ResponseBody Object saveOrder(@RequestBody List<CartItem> list){

get pathvariable with sending and recieving json data Rest spring mvc

I found the solution to my problem. There were many mistakes.

(1)The first problem was spring security which was authenticating the url and redirecting it to login page.

I changed this

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

to

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

(2)Changed myAction1 method to

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

removed an extra slash after {param1}

All that information I got from curl -i parameter

Before solving problem

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)

After Solving problem

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"}]

cheers

Spring MVC, pass json object to controler and return view with model attributes set

@RequestMapping(value = "submitReportQuery", method = RequestMethod.POST, consumes="application/json" )
public String submitReportQuery(@RequestBody ReportQueryMapBean reportQueryMap)throws Exception{
//model.addAttribute("successful", true);
return "queries/SelfServiceQueriesSubmitResults";
}

public class ReportQueryMapBean {
// to do delcare the bean fields to match the request
// and corresponding getter and setter function
}

How to send data from input id in Form to RequestMapping and PathVariable in Spring?

Hii I have tried different methods to send data from input to URL but it is not working then I have tried using javascript if you have no restrictions to use js you can do like this but it will work only if you have one data to send so if you use to model the work will be done quickly or you can try like this.

<form id="insBasicForm" name="insBasicForm"  method="post">
<input type="text" id="mobilePhoneNumber" name="mobilePhoneNumber" maxlength="10" placeholder="Telephone Number (Mobile- 10 Digits)" class="w100p" />
<input type="button" name="" value="submit" class="btn" onclick="createUrl()">
<script type="text/javascript">
function createUrl(){
let mobilePhoneNumber = document.getElementById("mobilePhoneNumber").value;
window.location = "/api/v1/checkPhoneNumber/"+mobilePhoneNumber;
}


Related Topics



Leave a reply



Submit