Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SpringMVC处理任意json对象?_Java_Json_Spring Mvc - Fatal编程技术网

Java 如何使用SpringMVC处理任意json对象?

Java 如何使用SpringMVC处理任意json对象?,java,json,spring-mvc,Java,Json,Spring Mvc,我成功地将SpringMVC与json结合使用,以便在域对象和json对象之间进行转换 现在,我想编写一个控制器,它只接受任何json,对其进行验证,并以紧凑的可序列化形式为服务层提供它。(json字符串就足够了,任何紧凑的字节数组表示都更好)。我目前的做法是: @RequestMapping(value="/{key}", method=RequestMethod.GET) @ResponseBody public Object getDocument(@PathVariable("usern

我成功地将SpringMVC与json结合使用,以便在域对象和json对象之间进行转换

现在,我想编写一个控制器,它只接受任何json,对其进行验证,并以紧凑的可序列化形式为服务层提供它。(json字符串就足够了,任何紧凑的字节数组表示都更好)。我目前的做法是:

@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);
}

@RequestMapping(value=“/{key}”,method=RequestMethod.PUT)
public void putDocument(@PathVariable(“用户名”)字符串用户名,
@路径变量(“键”)字符串键,
@请求体映射文件,
HttpServletRequest请求,
HttpServletResponse(响应){
LOGGER.info(createAccessLog(请求));
containerService.createOrUpdate(用户名、密钥、文档);
}
请注意,这种方法不起作用,因为我不希望put方法中有映射,get方法只返回{“this”:null};。我必须如何配置我的方法

干杯


Jan

Spring自动具有此功能。您只需要在类路径上添加
和jackson。然后,spring将通过json映射器处理accept头设置为
*/json
的所有请求和相应的响应。

很简单。您不需要
@RequestBody
注释

    @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);
    }

Bozoho,JSON映射不是问题,但是我需要什么方法签名才能获得一个可以序列化的通用JSON对象?我目前只使用public void put(@RequestBody MySpecificBean bean)从JSON中获取bean。那么,以后您将如何处理任意对象呢;我只想序列化它并将其存储在数据库中。客户端稍后可以从服务器请求它。所以任何字符串/字节[]/流都足够了,它应该是紧凑的。今天我可能更愿意使用
@RequestBody byte[]
    @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);
    }