0

有人可以告诉我为什么我从 Spring 获得 HTTP 状态 406 吗?我正在尝试让 spring 返回一些 json 代码,并且我正在恢复 406 状态..

这是我的控制器代码:

@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public  User getDisplayDefault(ModelMap model) {
      return new User("user name", "userid");
}

现在这是我的javaconfig:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.xxxx.mvc.web"})
public class WebMVCConfiguration extends WebMvcConfigurerAdapter
{

    private static final Logger logger = LoggerFactory.getLogger(WebMVCConfiguration.class);

    @Bean
    public ViewResolver resolver()
    {
        UrlBasedViewResolver url = new UrlBasedViewResolver();
        url.setPrefix("/views/");
        url.setViewClass(JstlView.class);
        url.setSuffix(".jsp");
        return url;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        logger.debug("setting up resource handlers");
        registry.addResourceHandler("/resources/").addResourceLocations("/resources/**");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    {
        logger.debug("configureDefaultServletHandling");
        configurer.enable();
    }

    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver()
    {
        SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();

        Properties mappings = new Properties();
        mappings.put("org.springframework.web.servlet.PageNotFound", "p404");
        mappings.put("org.springframework.dao.DataAccessException", "dataAccessFailure");
        mappings.put("org.springframework.transaction.TransactionException", "dataAccessFailure");
        b.setExceptionMappings(mappings);
        return b;
    }
}

所以我真的不明白为什么我的json没有回来

4

1 回答 1

1

你的类路径上有 Jackson/Jackson2 库吗?如果不是,Spring 将不会创建任何消息转换器。从文档:

  • MappingJackson2HttpMessageConverter(或 MappingJacksonHttpMessageConverter)与 JSON 相互转换——如果 Jackson 2(或 Jackson)出现在类路径中,则添加。
于 2013-06-06T14:54:15.690 回答