weixin_54912568 2024-06-24 21:04 采纳率: 66.7%
浏览 46
已结题

springmvc重定向和返回json

同一个method既想可以重定向,又可以把字符串对象比如“hello world”输出到页面!
现在在方法上追加@ResponseBody标签,会是原本重定向变成了文本输出到页面了
有什么好的解决办法吗

  • 写回答

26条回答 默认 最新

  • 专家-吕布[在线] 2024-06-24 23:39
    关注

    引自当下最先进的AI大模型:ChatGPT 4o,若有帮助,还望采纳。


    在Spring MVC中,重定向和返回JSON或字符串通常是互斥的操作,因为重定向意味着HTTP响应是一个302状态码,并且包含一个Location头,而返回JSON或字符串则意味着返回一个200状态码,并在响应体中包含数据。

    但是,有时候我们需要在某些情况下重定向,在某些情况下返回JSON或字符串。为了实现这个需求,可以采取以下几种方法:

    方法一:使用条件逻辑

    可以在方法中添加条件逻辑,根据条件决定返回重定向视图还是返回JSON或字符串。使用ResponseEntity可以灵活地返回不同类型的响应。

    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    
    @Controller
    public class MyController {
    
        @RequestMapping(value = "/myMethod", method = RequestMethod.GET)
        public ResponseEntity<?> myMethod(@RequestParam(name = "redirect", required = false) Boolean redirect) {
            if (Boolean.TRUE.equals(redirect)) {
                return ResponseEntity.status(HttpStatus.FOUND)
                                     .header("Location", "/newUrl")
                                     .build();
            } else {
                return ResponseEntity.ok("hello world");
            }
        }
    }
    

    方法二:使用两种不同的路径

    也可以通过使用不同的路径来区分重定向和返回JSON或字符串的逻辑。这种方法可以避免在同一个方法中混合两种不同的逻辑。

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class MyController {
    
        @RequestMapping(value = "/redirectMethod", method = RequestMethod.GET)
        public String redirectMethod() {
            return "redirect:/newUrl";
        }
    
        @RequestMapping(value = "/jsonMethod", method = RequestMethod.GET)
        @ResponseBody
        public String jsonMethod() {
            return "hello world";
        }
    }
    

    方法三:通过返回视图和数据包装类

    可以创建一个自定义的返回类型,根据不同的情况返回重定向视图或数据。使用这种方法,可以通过一个方法处理不同的返回类型。

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class MyController {
    
        @RequestMapping(value = "/myMethod", method = RequestMethod.GET)
        public ModelAndView myMethod(@RequestParam(name = "redirect", required = false) Boolean redirect) {
            ModelAndView modelAndView = new ModelAndView();
            if (Boolean.TRUE.equals(redirect)) {
                modelAndView.setViewName("redirect:/newUrl");
            } else {
                modelAndView.addObject("message", "hello world");
                modelAndView.setViewName("dataView");  // 假设这是一个处理JSON输出的视图
            }
            return modelAndView;
        }
    }
    

    在上面的例子中,dataView 可以是一个处理JSON输出的视图,你可以自定义它来返回JSON或字符串。

    总结

    通过以上几种方法,可以实现根据条件在同一个方法中既能重定向又能返回JSON或字符串。可以根据具体需求选择适合的实现方式。如果有任何进一步的问题或需要更详细的解释,请随时联系我。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(25条)

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 修改了问题 6月24日
  • 创建了问题 6月24日