Spring mvc controller issue

I'm trying to learn spring MVC so far so good, but I'm stuck right now. I am trying to learn how to create json and get it using javascript (jquery).

But for testing purposes, I tried to create something so that I can see that it is displayed correctly through an HTTP request, then I will try to create json and get it, but so far I can’t even get a request for work, Here is my controller:

@Controller @RequestMapping(value="/") public class IndexController { @RequestMapping(method=RequestMethod.GET) public String index() { return "index"; } Map<Long,Item> itemMap = createItemMap(); @RequestMapping(value="item/{itemId}", method=RequestMethod.GET) public @ResponseBody Item get(@PathVariable Long itemId) { Item item = itemMap.get(itemId); if (status == null) { throw new ResourceNotFoundException(itemId); } return item; } private Map<Long,Item> createItemMap(){ //omitted because its irrelevant //I created 2 item objects , with id 1 and 2 for testing purposes } } 

This is the contents of my servlet-context.xml:

 <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- DispatcherServlet Context: defines this servlet request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <!-- Imports user-defined @Controller beans that process client requests --> <beans:import resource="controllers.xml" /> </beans:beans> 

And controllers.xml:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Scans within the base package of the application for @Components to configure as beans --> <context:component-scan base-package="com.testing.mvc.controller" /> <!-- Application Message Bundle --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="/WEB-INF/messages/messages" /> <property name="cacheSeconds" value="0" /> </bean> </beans> 

My war is called Test.war, when I try localhost:8080/Test , I get the index code, which is fine. But no matter what I try:

 localhost:8080/Test/item/1 localhost:8080/Test/item?itemId=1 localhost:8080/item?itemId=1 

As a result, I get some error, the most interesting one is this:

 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers (). 

I have a lot of googled found that they are interesting:

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ https://src.springframework.org/svn/spring-samples/mvc-ajax/trunk/ Mapping remaining ajax requests on spring Spring Json will not be allowed with the corresponding response

Nothing has helped so far, any idea that I am missing. Sorry for providing too much information.

+4
source share
1 answer

As far as I can tell, in your dispatching serlet there is no such resolver:

 <bean name="jsonViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="order" value="1"/> </bean> 

Take a look at this:
http://spring-json.sourceforge.net/quick_simpleform.html

You need to create the views.xml file in yoru WEB-INF direcotry with this content:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/> </beans> 
+2
source

Source: https://habr.com/ru/post/1335633/


All Articles