guyeuro
V2EX  ›  问与答

SpringMVC: controller 中没有 @ResponseBody 注解能返回字符串?

  •  
  •   guyeuro · Aug 29, 2017 · 5699 views
    This topic created in 3369 days ago, the information mentioned may be changed or developed.

    我在看一个 java web 工程,用的 SpringMVC

    有些 controller 中没有 @ResponseBody 注解,返回字符串

        return JSONObject.toJSONString(obj);
    

    按照配置应该是返回 jsp 页面

    但是返回的是字符串

    这里有何玄机?

    9 replies  •  2017-08-29 18:49:00 +08:00
    CoderGeek
    CoderGeek
    Aug 29, 2017
    1
    return new ModelAndView("/xx/xxx/xx");
    返回视图
    ooTwToo
    ooTwToo
    Aug 29, 2017
    2
    是不是在 Class 上面加了 @RestController ? 没有的话可能设置了 respone 的 content-type
    EXE
    EXE
    Aug 29, 2017
    3
    RestController?
    Goooogle
    Goooogle
    Aug 29, 2017
    4
    一种可能是使用了 ContentNegotiatingViewResolver,配置了 MappingJackson2JsonView
    guyeuro
    guyeuro
    OP
    Aug 29, 2017
    5
    @ooTwToo 没有,就是 RequestMapping
    songjiaxin2008
    songjiaxin2008
    Aug 29, 2017
    6
    @ooTwToo 你还有时间刷 v2??? 工作不饱和
    ooTwToo
    ooTwToo
    Aug 29, 2017
    7
    @songjiaxin2008 忙里偷闲!
    qinxi
    qinxi
    Aug 29, 2017
    8
    WebMvcConfigurerAdapter.class
    -----
    configureMessageConverters(converters) {

    converters.add(MappingJackson2HttpMessageConverter);
    }
    misaka19000
    misaka19000
    Aug 29, 2017 via Android
    9
    把源码贴出来
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1104 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 23:19 · PVG 07:19 · LAX 15:19 · JFK 18:19
    ♥ Do have faith in what you're doing.