0

I successfully use spring-mvc with json in order to convert between domain objects and json objects.

Now, I want to write a Controller that just accepts any json, validates it and provides it in a compact serialisable form for the service layer. (The json string would be sufficient, any compact byte array representation better). My current approch is this:

@RequestMapping(value="/{key}", method=RequestMethod.GET)
@ResponseBody
public Object getDocument(@PathVariable("username") String username,
        @PathVariable("key") String key,
        HttpServletRequest request,
        HttpServletResponse response) {
    LOGGER.info(createAccessLog(request));
    Container doc = containerService.get(username, key);
    return jacksonmapper.map(doc.getDocument(), Map.class);
}

and

@RequestMapping(value="/{key}", method=RequestMethod.PUT)
public void putDocument(@PathVariable("username") String username,
        @PathVariable("key") String key,
        @RequestBody Map<String,Object> document,
        HttpServletRequest request,
        HttpServletResponse response) {
    LOGGER.info(createAccessLog(request));
    containerService.createOrUpdate(username, key,document);
}

Note that this approach does not work because I don't want a Map in the put method and the get method returns just {"this":null};. How do I have to configure my methods?

Cheers,

Jan

2 Answers 2

2

Spring has this functionality automatically. You just need <mvc:annotation-driven /> and jackson on your classpath. Then spring will handle all requests with accept header set to */json, and respective responses, through the JSON mapper.

Sign up to request clarification or add additional context in comments.

3 Comments

Bozoho, the JSON mapping is not the problem but what method signature do I need to get a generic json object which I can serialize? I currently only work with public void put(@RequestBody MySpecificBean bean) to get a bean from JSON.
well, how will you afterward handle the arbitrary object?
Late answer, was on vacation; I only want to serialize it and store it in a database. The client can later request it from the server. So any String/byte[]/Stream is sufficient it just should be compact.
0

Its easy. You don't need the @RequestBody annotation.

    @RequestMapping(value="/{key}", method=RequestMethod.PUT)
    public void putDocument(@PathVariable("username") String username,
            @PathVariable("key") String key, HttpServletRequest request, HttpServletResponse response) {

        try {
            String jsonString = IOUtils.toString(request.getInputStream()); 
        } catch (IOException e) {
            e.printStackTrace();
        }

        LOGGER.info(createAccessLog(request));
        containerService.createOrUpdate(username, key,document);
    }

1 Comment

I'd probably rather use @RequestBody byte[] today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.