Accepting / returning XML/JSON request and response - Spring MVC

前端 未结 5 758
無奈伤痛
無奈伤痛 2021-01-31 04:54

I need to write a rest service which accepts XML/JSON as a input (POST method) and XML/JSON as a output (based on the input format). I have tried a below approach to achieve thi

5条回答
  •  长发绾君心
    2021-01-31 05:43

    Adding to Manish's answer above, if you don't wanna use xml based configuration use this java based configuration instead-

    @Bean
    public ViewResolver contentNegotiatingViewResolver() {
        ContentNegotiatingViewResolver resolver =
                new ContentNegotiatingViewResolver();
    
        List views = new ArrayList<>();
        views.add(new MappingJackson2XmlView());
        views.add(new MappingJackson2JsonView());
    
        resolver.setDefaultViews(views);
        return resolver;
    }
    

提交回复
热议问题