我正在使用Spring MVC 4,我想知道我在課堂上使用@RestController
,並且我正在使用@RequestMapping(headers = "Accept=application/json")
。現在任何人都可以告訴我爲什麼只需在我的config-servlet.xml
中使用<mvc:annotation-driven />
我如何在瀏覽器中獲得JSON
的響應。我的意思是使用頭文件屬性只是說這個方法只能迴應具有這種頭文件的請求。 MVC4如何知道在瀏覽器上回復JSON
。這裏是我的代碼:在SpringMVC4中使用@RequestMapping(headers =「Accept = application/json」)
我的配置-servlet.xml中
<beans>
<context:component-scan base-package="com.songs.service.controller" />
<mvc:annotation-driven />
</beans>
我RestController類
@RestController
@RequestMapping("/songappWS")
public class SongServiceWS
{
@RequestMapping(value = "/topmoviesWS", method = RequestMethod.GET, headers="Accept=application/json")
public List<?> getTopMoviesWS() {
//method logic
return l1;
}
}
這是四季如春會自動選擇當'@ ResponseBody'使用'RestController'時存在或默認 – user3626306
在JSON返回,如果傑克遜是在類路徑中,那麼是的,Spring MVC的將返回JSON – geoand