简体   繁体   中英

Is there in spring mvc abstractions over HttpServletResponse?

When I use spring mvc I don't use raw HttpServletRequest . Instead of this I use Model .

Is there similar way to avoid using HttpServletResponse ?

I'm not sure I get your question.

Spring MVC is deriving the HttpServletResponse from whatever your controller method returns, and exception handlers (if a business exception was thrown by that method), interceptors, HttpMessageConverters, etc. Maybe half of the Spring MVC reference documentation deals with that.

Maybe looking at the supported method return types in Controllers will answer your question?

Also, your Controller method can return a ResponseEntity<?> type, which you can use like this:

@Controller
public class MyController {

  @RequestMapping("/test")
  public ResponseEntity<String> test() {
    return ResponseEntity.status(HttpStatus.OK).header("X-Foo", "Bar").body("Hello World!");
  }

  @RequestMapping(value= "/other", produces = "application/json")
  public ResponseEntity<User> other() {
    User user = new User("firstName", "lastName");
    // here, Spring MVC will use a configured HttpMessageConverter to convert your model
    // instance to JSON, for example
    return ResponseEntity.status(HttpStatus.OK).body(user);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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