2015-12-02 119 views
0

我創建使用Spring 3與Hibernate 5.彈簧,JPA和Hibernate - 不需要JSON文件擴展名

我Maven依賴這些簡單的Web服務REST風格的終點是:

 <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>3.2.14.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-oxm</artifactId> 
      <version>3.2.14.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
      <version>5.2.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>5.0.1.Final</version> 
     </dependency> 

這不是我完全依賴的列表,但它提供了我正在使用的版本的一個好主意。

該Web服務的基本要求是提供一個端點,用戶可以通過該端點請求一些基於唯一ID的數據。數據必須以JSON形式返回。數據正在從SQL Server 2008數據庫視圖中檢索。

我已成功配置Web服務以使用Hibernate和JPA來獲取正確的數據,其中ID在視圖中的行上匹配。該ID是作爲與URL參數,例如:正如我所說的現在

http://some/resource/location.json?id=1234 

,這工作得很好,如果ID匹配,並返回編組爲JSON給用戶一個POJO獲取數據。

我的問題是需要包含'.json'文件擴展名作爲網址的一部分。理想情況下,我想的URL看起來像這樣代替:

http://some/resource/location?id=1234 

通知沒有「以.json」

我使用的配置如下的視圖解析器,只是忽略了XML的東西,我有在那裏,因爲我將需要更進一步的下線:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <property name="order" value="1"/> 
     <property name="contentNegotiationManager"> 
      <bean class="org.springframework.web.accept.ContentNegotiationManager"> 
       <constructor-arg> 
        <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy"> 
         <constructor-arg> 
          <map> 
           <entry key="json" value="application/json"/> 
           <entry key="xml" value="application/xml"/> 
          </map> 
         </constructor-arg> 
        </bean> 
       </constructor-arg> 
      </bean> 
     </property> 
     <property name="defaultViews"> 
      <list> 
       <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/> 
       <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> 
        <constructor-arg> 
         <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> 
          <property name="autodetectAnnotations" value="true"/> 
         </bean> 
        </constructor-arg> 
       </bean> 
      </list> 
     </property> 
    </bean> 

是否有一個不同的視圖解析器我應該實現也許?

我可以包括更多的細節,如果需要,只是問,謝謝你的任何幫助。

回答

1

檢查this spring.io教程。它會適合您的需要,更特地這個XML配置,因爲我看到您使用XML配置而不是Java的配置:

<!-- Total customization - see below for explanation. --> 
    <bean id="contentNegotiationManager" 
      class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="favorPathExtension" value="true" /> 
    <property name="favorParameter" value="false" /> 
    <property name="parameterName" value="mediaType" /> 
    <property name="ignoreAcceptHeader" value="true"/> 
    <property name="useJaf" value="false"/> 
    <property name="defaultContentType" value="application/json" /> 

    <property name="mediaTypes"> 
     <map> 
      <entry key="json" value="application/json" /> 
      <entry key="xml" value="application/xml" /> 
     </map> 
    </property> 
</bean> 

我想我定製適合您的需求。

+1

好吧,我+1你的答案,但它不像我希望從你鏈接的教程那樣切割和乾燥。然而,我最終通過提取一個簡單的小部分來得到答案,我將在下面詳細介紹。 – Jeremy

0

Nikolay pointed me in the right direction,但實際的答案是tutorial的這樣一個小部分,我認爲這將是對別人尋找類似的答案顯示正是解決辦法,我到底在這裏使用更有幫助。

基本上關鍵是一個produces = {"application/json"} PARAM添加到我映射到一個URL,像這樣我的控制器方法:

@RequestMapping(
      value = "/some/resource/location", 
      method = RequestMethod.GET, 
      produces = {"application/json"} 
    ) 
    public @ResponseBody CustomClass getSomething(@RequestParam String id) { 
     return customService.getSomethingById(id); 
    } 

另外,我不得不與我的servlet URL映射相當具體,遠離移動通過擴展映射到特定URI的映射。我敢肯定,這可以做不同的,但它的工作對我的具體要求:

<servlet> 
    <servlet-name>myServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/config/servlet-config.xml</param-value> 
    </init-param> 
</servlet> 

<servlet-mapping> 
    <servlet-name>myServlet</servlet-name> 
    <url-pattern>/some/resource/location</url-pattern> 
</servlet-mapping> 

我要指出,我得到了一些幫助,從另一個堆棧溢出answer discussing what values the url pattern would except servlet的URL模式配置。

就是這樣,它只是工作,畢竟不需要包含特殊的內容談判bean。

我簡單地使用終點是這樣的:

http://some/resource/location?id=1234 

,並返回JSON。