简体   繁体   English

Spring MVC @ResponseBody问题

[英]Spring MVC @ResponseBody issue

I just learn about spring mvc for few days and there are some codes I wrote : 我刚刚学习了有关spring mvc的几天,我写了一些代码:

@RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET}, consumes = "*/*", produces = "application/json")
@ResponseBody
public  Object userLogin(@RequestParam(value = "userName") String userName,@RequestParam(value = "password") String password) {

    String password2 = userService.selectUserPassword(userName);

    JSONObject object = new JSONObject();
    if(password2.equals(password)){
        object.put("login", "true");
    }else{
        object.put("login", "false");
    }
    return object;  

}

It doesn't work, but if I change the code to this 它不起作用,但是如果我将代码更改为此

public  Map<String, String> userLogin(@RequestParam(value = "userName") String userName,
        @RequestParam(value = "password") String password) {

    String password2 = userService.selectUserPassword(userName);

    Map<String, String> map = new HashMap<String, String>(1);
    //JSONObject object = new JSONObject();
    if(password2.equals(password)){
        map.put("login", "true");
    }else{
        map.put("login", "false");
    }
    return map; 

}

then it works. 然后就可以了。 So I was wondering why? 所以我想知道为什么?

To understand a difference between your two methods, note the following 要了解两种方法之间的区别,请注意以下几点

  • The @ResponseBody annotation indicates that the return type is written to the response body. @ResponseBody批注指示将返回类型写入响应主体。

  • Spring converts the returned object to a response body by using an appropriate HttpMessageConverter Spring通过使用适当的HttpMessageConverter将返回的对象转换为响应主体

  • Since you've indicated the produces = "application/json" the converter that will kick in is MappingJackson2HttpMessageConverter An HttpMessageConverter implementation that can read and write JSON using Jackson's ObjectMapper 既然您已经指示了Produces produces = "application/json" ,那么将要启动的转换器就是MappingJackson2HttpMessageConverter 一个HttpMessageConverter实现,可以使用Jackson的ObjectMapper读取和写入JSON

this is a powerfull feature that Spring MVC does automatically, it resolve the representation, and converts automatically. 这是Spring MVC自动执行的强大功能,它可以解析表示形式并自动进行转换。

In your first method you've attempted doing this manually which is not needed and wrong. 在第一种方法中,您尝试手动执行此操作,这是不必要且错误的。 What the framework attempted is to convert your JsonObject to JSON representation, which either fails or gives you JSON properties containing the fileds of the JsonObject class. 框架试图将JsonObject转换为JSON表示形式,这将失败或为您提供包含JsonObject类文件的JSON属性。

In the later method you poplated the values to a Map, and let the Spring MVC convert to JSON. 在后面的方法中,将值填充到Map中,然后让Spring MVC转换为JSON。 As Jackson libraries knows how to convert the Map to JSON, your later method works without issues 由于Jackson库知道如何将Map转换为JSON,因此您的后续方法可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM