返回的web.xml對象:春406錯誤,而試圖在
<servlet>
<servlet-name>rest-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
的pom.xml:
...
<jackson.mapper.version>1.9.13</jackson.mapper.version>
...
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>${jackson.mapper.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>${jackson.mapper.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>${jackson.mapper.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.mapper.version}</version>
</dependency>
休息服務-config.xml中:
<mvc:annotation-driven />
<context:component-scan base-package="com.example.web.rest" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="contentType" value="text/plain"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonMessageConverter"/>
</util:list>
</property>
</bean>
<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
rest-dispatcher-servlet.xml:
<context:annotation-config />
<context:component-scan base-package="com.example.web.rest" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.html</value>
</property>
</bean>
<mvc:resources mapping="/WEB-INF/pages/**" location="/WEB-INF/pages/" />
<mvc:resources mapping="/vendors/**" location="/resources/vendors/" />
<mvc:resources mapping="/controls/**" location="/resources/controls/" />
<mvc:resources mapping="/css/**" location="/resources/css" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
最後,這裏是我的控制器:
@Controller
@RequestMapping("/user")
public class UserService {
@Autowired(required=true)
private UserDaoImpl dao;
@RequestMapping(value="/getCurrentUserCredentials/", method = RequestMethod.GET)
public @ResponseBody UserCredentials getCurrentUserCredentials() {
HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
try {
return dao.getCurrentUserCredentials((Integer)session.getAttribute("userid"));
} catch (Exception e) {
return null;
}
}
}
...並要求這給我一個406error:
$.ajax({
type: 'GET',
url: 'user/getCurrentUserCredentials/',
dataType: 'json',
success: function(data) {console.log(data); self.currentUserCredentials(data);},
error: function() {console.log('error');}
});
有關解決這個有什麼建議?請幫助。
控制器的方法返回正確的數據(在調試中檢查),同時無法正確處理此數據。
嘗試將rest-services-config.xml中的MappingJacksonView更改爲 ' –
geoand
不幸的是,」406「仍然 – Arti88
'getCurrentUserCredentials'是否拋出異常? – geoand