Spring MVC控制器:如何优雅地返回HTTP响应

在Spring MVC中,返回HTTP响应通常涉及控制器(Controller)的方法返回特定的视图名称或直接写入HTTP响应。以下是一些常见的实现方式:

图片[1]_Spring MVC控制器:如何优雅地返回HTTP响应_知途无界

1. 返回视图名称

最常见的方式是控制器方法返回一个字符串,该字符串表示视图的名称。Spring MVC会解析这个字符串并渲染相应的视图。

@Controller
public class MyController {
@RequestMapping("/hello")
public String sayHello(Model model) {
model.addAttribute("message", "Hello, World!");
return "hello"; // 返回视图名称,例如 hello.jsp 或 hello.html
}
}
@Controller
public class MyController {

    @RequestMapping("/hello")
    public String sayHello(Model model) {
        model.addAttribute("message", "Hello, World!");
        return "hello"; // 返回视图名称,例如 hello.jsp 或 hello.html
    }
}
@Controller public class MyController { @RequestMapping("/hello") public String sayHello(Model model) { model.addAttribute("message", "Hello, World!"); return "hello"; // 返回视图名称,例如 hello.jsp 或 hello.html } }

在这个例子中,Spring MVC会查找名为 hello 的视图模板(例如 hello.jsp 或 hello.html),并将模型数据传递给视图进行渲染。

2. 返回ModelAndView对象

ModelAndView对象封装了视图名称和模型数据,可以直接返回。

@Controller
public class MyController {
@RequestMapping("/hello")
public ModelAndView sayHello() {
ModelAndView modelAndView = new ModelAndView("hello");
modelAndView.addObject("message", "Hello, World!");
return modelAndView;
}
}
@Controller
public class MyController {

    @RequestMapping("/hello")
    public ModelAndView sayHello() {
        ModelAndView modelAndView = new ModelAndView("hello");
        modelAndView.addObject("message", "Hello, World!");
        return modelAndView;
    }
}
@Controller public class MyController { @RequestMapping("/hello") public ModelAndView sayHello() { ModelAndView modelAndView = new ModelAndView("hello"); modelAndView.addObject("message", "Hello, World!"); return modelAndView; } }

3. 返回ResponseEntity对象

ResponseEntity对象允许你完全控制HTTP响应,包括状态码、头部信息和响应体。

@Controller
public class MyController {
@RequestMapping("/hello")
public ResponseEntity<String> sayHello() {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/plain");
String responseBody = "Hello, World!";
return new ResponseEntity<>(responseBody, headers, HttpStatus.OK);
}
}
@Controller
public class MyController {

    @RequestMapping("/hello")
    public ResponseEntity<String> sayHello() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "text/plain");
        String responseBody = "Hello, World!";
        return new ResponseEntity<>(responseBody, headers, HttpStatus.OK);
    }
}
@Controller public class MyController { @RequestMapping("/hello") public ResponseEntity<String> sayHello() { HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "text/plain"); String responseBody = "Hello, World!"; return new ResponseEntity<>(responseBody, headers, HttpStatus.OK); } }

在这个例子中,返回的是一个纯文本响应,HTTP状态码为200(OK)。

4. 使用@ResponseBody注解

@ResponseBody注解告诉Spring MVC将方法的返回值作为响应体直接写入HTTP响应。

@Controller
public class MyController {
@RequestMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, String> sayHello() {
Map<String, String> response = new HashMap<>();
response.put("message", "Hello, World!");
return response;
}
}
@Controller
public class MyController {

    @RequestMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String, String> sayHello() {
        Map<String, String> response = new HashMap<>();
        response.put("message", "Hello, World!");
        return response;
    }
}
@Controller public class MyController { @RequestMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map<String, String> sayHello() { Map<String, String> response = new HashMap<>(); response.put("message", "Hello, World!"); return response; } }

在这个例子中,返回的是一个JSON格式的响应体。注意这里使用了produces = MediaType.APPLICATION_JSON_VALUE来指定响应的内容类型为JSON。

5. 使用HttpServletResponse对象

你也可以直接在控制器方法中访问HttpServletResponse对象,并手动写入响应。

@Controller
public class MyController {
@RequestMapping("/hello")
public void sayHello(HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.getWriter().write("Hello, World!");
}
}
@Controller
public class MyController {

    @RequestMapping("/hello")
    public void sayHello(HttpServletResponse response) throws IOException {
        response.setContentType("text/plain");
        response.getWriter().write("Hello, World!");
    }
}
@Controller public class MyController { @RequestMapping("/hello") public void sayHello(HttpServletResponse response) throws IOException { response.setContentType("text/plain"); response.getWriter().write("Hello, World!"); } }

在这个例子中,通过HttpServletResponse对象手动设置响应的内容类型和写入响应体。

总结

以上几种方式各有优缺点,选择哪种方式取决于你的具体需求。如果你需要完全控制HTTP响应,ResponseEntityHttpServletResponse是不错的选择。如果你只是需要返回视图和模型数据,返回视图名称或ModelAndView对象会更简洁。

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞71 分享
Real dream is the other shore of reality.
真正的梦就是现实的彼岸
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容