2014-04-30 74 views
1
@ResponseBody我

返回的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');} 
     }); 

有關解決這個有什麼建議?請幫助。

控制器的方法返回正確的數據(在調試中檢查),同時無法正確處理此數據。

+0

嘗試將rest-services-config.xml中的MappingJacksonView更改爲 ' – geoand

+0

不幸的是,」406「仍然 – Arti88

+0

'getCurrentUserCredentials'是否拋出異常? – geoand

回答

0

你不打算在這裏添加@Produces註釋嗎?

@RequestMapping(value="/getCurrentUserCredentials/", method = RequestMethod.GET, produces="application/json") 
public @ResponseBody UserCredentials getCurrentUserCredentials() { 
    HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession(); 
    try { 
     return dao.getCurrentUserCredentials((Integer)session.getAttribute("userid")); 
    } catch (Exception e) { 
     return null; 
    } 
} 

不確定這是否重要,但@ResponseBody註釋也在public關鍵字之前。我的例子是基於這個:

@Controller 
public class ErrorController { 

    @RequestMapping(value="/error", produces="application/json") 
    @ResponseBody 
    public Map<String, Object> handle(HttpServletRequest request) { 

     Map<String, Object> map = new HashMap<String, Object>(); 
     map.put("status", request.getAttribute("javax.servlet.error.status_code")); 
     map.put("reason", request.getAttribute("javax.servlet.error.message")); 

     return map; 
    } 

} 

還有有@RestController而不是@Controller春季的較新版本的選項。

+0

嗨,試圖在「public ...」之前加上「@ResponseBody」,但似乎沒有關係。增加了「產品」參數,但仍然是406. – Arti88

+0

@ Arti88,你是否也試過請求映射的產生部分(即'produce =「application/json」')? –

+0

是啊...仍然是一樣的:加載資源失敗:服務器響應狀態爲406(不可接受)http:// localhost:8080/PR2/user/getCurrentUserCredentials / – Arti88