【问题标题】:Spring, JPA and Hibernate - RESTFul end point that does not require the json file extensionSpring、JPA 和 Hibernate - 不需要 json 文件扩展名的 RESTFul 端点
【发布时间】:2015-12-02 13:22:42
【问题描述】:

我正在使用 Spring 3 和 Hibernate 5 创建一个简单的 Web 服务。

我的 Maven 依赖项是:

        <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 的一部分。理想情况下,我希望 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>

我应该实现不同的视图解析器吗?

如果需要,我可以提供更多详细信息,请询问,感谢您对此提供的任何帮助。

【问题讨论】:

    标签: java json spring hibernate jpa


    【解决方案1】:

    查看thisspring.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 你的答案,但它并不像我从你链接的教程中所希望的那样干脆利落。不过,我最终还是通过提取其中的一小部分简化部分得到了答案,接下来我将在此处详细说明。
    【解决方案2】:

    Nikolay pointed me in the right direction,但实际答案只是tutorial 的一小部分,我认为这对寻找类似答案的其他人会更有帮助,以准确显示我最终在这里使用的解决方案。

    基本上,关键是将produces = {"application/json"} 参数添加到我映射到 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-10-04
      • 1970-01-01
      • 2013-12-23
      相关资源
      最近更新 更多