Announcement Announcement Module
Collapse
No announcement yet.
How to easily parse encoded JSON using Jackson from MVC controller? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to easily parse encoded JSON using Jackson from MVC controller?

    I have an MVC controller whose signature looks like:

    @RequestMapping(value = "/state/flow/{target}", method = RequestMethod.POST)
    public String handleFlowTransition(@PathVariable String target, @RequestBody String trace,
    HttpServletRequest request) {
    ...
    }


    When my client posts a request, I see that trace contains an encoded JSON string much like the following (as expected):

    [
    {"id":"1000","result":null,"retries":0,"timeouts": 0},
    {"id":"1050","result":null,"retries":0,"timeouts": 0},
    {"id":"1080","result":"2","retries":0,"timeouts":0 },
    {"id":"1090","result":null,"retries":0,"timeouts": 0},
    {"id":"1100","result":null,"retries":0,"timeouts": 0},
    {"id":"1500","result":"1","retries":0,"timeouts":0 }
    ]


    All I want to do is decode and parse this to an array or list of "Module" objects.

    I have the Jackson parser set up within my pom.xml file and see no problems there. I've also tried the following (based on some examples I found online), but I get a 415 HTTP response (unsupported media type).

    Looking at the Jackson documentation, I see that I can probably just explicitly call on the Jackson utilities to do this, but isn't there a more elegant "Springy" way of doing this (relying on DI)? Am I missing an annotation somewhere?

    @RequestMapping(value = "/state/flow/{target}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public String handleFlowTransition(@PathVariable String target, @RequestBody Module[] trace,
    HttpServletRequest request) {
    ...
    }
Working...
X