1

I have got the following code:

public class MyClass {

    String xxx;
    String yyy;

    public String getXxx() {
        return xxx;
    }
    public void setXxx(String xxx) {
        this.xxx = xxx;
    }
    public String getYyy() {
        return yyy;
    }
    public void setYyy(String yyy) {
        this.yyy = yyy;
    }
    public MyClass(String xxx, String yyy) {
        super();
        this.xxx = xxx;
        this.yyy = yyy;
    }

    @Override
    public String toString() {
        return "MyClass [xxx=" + xxx + ", yyy=" + yyy + "]";
    }
}

I also implemented the service:

import org.springframework.stereotype.Controller;
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
class MyService {

    @RequestMapping(value = "/abc", method = RequestMethod.POST, produces = "application/json")

    public @ResponseBody
    String add(@RequestBody String myClass, HttpServletRequest request,
        HttpServletResponse response) {

        return "Test";
    }

}

When I do an HTTP call with DEV HTTP CLIENT with JSON:

{"xxx":"abc", "yyy":"abc"}

enter image description here

I see an error:

Error 406 NOT_ACCEPTABLE

Is it possible to do it in this way or I have to encode JSON and create a Java Object?

ruhungry
  • 4,506
  • 20
  • 54
  • 98
  • 2
    Do you have the Jackson libraries (http://jackson.codehaus.org/) available on your classpath? This should be seamless when they are present (with a relatively vanilla configuration). – nicholas.hauschild Feb 10 '14 at 15:36
  • No, it isn't but I am not sure if it is a solution – ruhungry Feb 10 '14 at 15:54
  • You keep using strings instead of your class. `@ResponseBody` should generally return the POJO representing the JSON response. Also, explicitly setting the `produces` value is redundant (and prevents Spring from returning XML, in case that matters). – chrylis -cautiouslyoptimistic- Feb 11 '14 at 10:59

2 Answers2

4

try to add produces="application/json" in @RequestMapping

import org.springframework.stereotype.Controller;
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
class MyService {

@RequestMapping(value = "/abc", method = RequestMethod.POST,produces = "application/json" )
    public @ResponseBody String myMethod( @RequestBody String _json,HttpServletRequest request,
        HttpServletResponse response) {
        return _json;
    }   
}

this the result with postman extension for chrome

sagat
  • 56
  • 3
1

you can do that with google GSON in your controller.

here is example:

            Gson gson = new Gson();
            JsonElement s = gson.toJsonTree(device, Devices.class);
            return s.toString();

and do not forget for @ResponseBody

Hope it will be helpfull

Daris
  • 331
  • 1
  • 10