SpringMVC data response (2)

4.6. SpringMVC's data response - write back data - write back string directly (application)

The response object injected through the SpringMVC framework uses response.getWriter().print("hello world") to write back the data. At this time, no view jump is required, and the return value of the business method is void.

Return the string that needs to be written back directly, but at this time you need to inform the SpringMVC framework through the @ResponseBody annotation that the string returned by the method is not a jump but is returned directly in the http response body

    @RequestMapping(value = "/login6")  //请求地址
    public void login6(HttpServletResponse response) throws IOException {
    
    
        response.setContentType("text/html;charset=UTF-8");//设置编码格式
        response.getWriter().print("你好!");
    }

    @RequestMapping(value = "/login7")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    public String login7() throws IOException {
    
    
        return "hello world";
    }

4.7. SpringMVC's data response - write back data - directly write back json format string (application)

    @RequestMapping(value = "/login8")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    public String login8() throws IOException {
    
    
        return "{\"username\":\"NanYu\",\"age\":18}";
    }

The method of manually splicing json format strings is very troublesome. In development, complex java objects are often converted into json format strings. We can use the json conversion tool jackson learned in the web stage to convert, and convert json format strings through jackson , write back the string

    @RequestMapping(value = "/login9")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    public String login9() throws IOException {
    
    
        User user = new User();
        user.setName("zhangsan");
        user.setAge("18");
        //使用json的转换工具将对象转换成json对象或字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String string = objectMapper.writeValueAsString(user);
        return string;
    }

4.8. SpringMVC's data response - write back data - return object or collection (application)

Through SpringMVC, we can convert and write back json strings to objects or collections, configure message conversion parameters for processor adapters, and specify the use of jackson to convert objects or collections, so we need to configure the following in spring-mvc.xml:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
            </list>
        </property>
    </bean>
 @RequestMapping(value = "/login10")  //请求地址
    @ResponseBody     //告诉springMVC框架,该方法不进行视图跳转,直接进行数据响应
    //期望springMVC自动将User转换成json格式的字符串
    public User login10() throws IOException {
    
    
        User user = new User();
        user.setName("zhangsan");
        user.setAge("18");
        return user;
    }

4.9. SpringMVC's data response - write back data - return object or collection 2 (application)

Adding @ResponseBody to the method can return a string in json format, but this configuration is more troublesome, and the configuration code is more, so we can use the mvc annotation driver to replace the above configuration

开启mvc的自动注解驱动
<mvc:annotation-driven/>

Among the various components of SpringMVC, the processor mapper , processor adapter , and view resolver are called the three major components of SpringMVC.

Use <mvc:annotation-driven />autoloading RequestMappingHandlerMapping (handling mapper) and

RequestMappingHandlerAdapter (processing adapter), which can be used in the Spring-xml.xml configuration file

<mvc:annotation-driven />Override configuration for annotation processors and adapters.

use simultaneously<mvc:annotation-driven />

By default, the bottom layer will integrate jackson to convert the json format string of the object or collection

4.10. SpringMVC's data response - summary of knowledge points (understanding, memory)

1) Page jump

return the string directly

Returned by the ModelAndView object

2) Write back data

return the string directly

The HttpServletResponse object directly writes back the data, the HttpServletRequest object brings back the data, the Model object brings back the data or @ResponseBody writes back the string data

return object or collection

@ResponseBody+<mvc:annotation-driven/>

Guess you like

Origin blog.csdn.net/qq_64071654/article/details/128411633
Recommended