繁体   English   中英

spring 5 如何在@pathvariable 中发送带有 a.json 后缀的参数

[英]spring 5 How do I send a parameter with a .json suffix in the @pathvariable

我有一个接收“long”作为参数类型的服务,但是发送带有 .json 后缀的参数并不能解析它,我把它当作一个“字符串”。 例如,如果我发送“1017.json”,它就好像要发送的参数是一个字符串而不是一个长字符串。

这是服务

@ResponseBody
@RequestMapping("changeProblem/{treatmentPlanGridId}")
public List<DropDownDTO> changeProblem(@PathVariable(value = "treatmentPlanGridId") long 
treatmentPlanGridId)
        throws ControllerException {
    List<DropDownDTO> subProblemDTOList = new ArrayList<DropDownDTO>();
    LocalDate activeDate = new LocalDate();

在我的调度程序中添加后缀属性,但它仍然不起作用

<?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"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:security="http://www.springframework.org/schema/security"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx.xsd">


 <mvc:annotation-driven content-negotiation-manager="cnManager" />

<context:annotation-config />

<!-- In the dispatcher context, we should only include classes annotated with the @Controller annotation -->
<context:component-scan base-package="com.qualifacts.carelogic" use-default-filters="false">
    <!-- this should only load controllers and their support components. all other spring beans are loaded by the parent app context -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    <context:include-filter type="annotation" expression="com.qualifacts.carelogic.ControllerSupport" />
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>





<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/client/ecr/**"/>
        <bean class="com.qualifacts.carelogic.client.ECRInterceptor"/>
    </mvc:interceptor>
    <mvc:interceptor>
        <mvc:mapping path="/svcdoc/document/pdfgenerator/**"/>
        <bean class="com.qualifacts.carelogic.svcdoc.pdf.PDFGeneratorInterceptor"/> 
    </mvc:interceptor>
</mvc:interceptors>

<bean id="xmlViewResolver" class="com.qualifacts.carelogic.spring.XmlViewResolver">
    <qualifier value="xmlResolver"></qualifier>
    <property name="delegate" ref="jspViewResolver"/>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1"/>
    <property name="contentNegotiationManager" ref="cnManager"/>
    <property name="viewResolvers">
        <list>
            <!-- xslt view resolver handles xml -> html view translation -->
            <bean class="com.qualifacts.carelogic.spring.XsltViewResolver">
                <property name="delegate" ref="jspViewResolver"/>
            </bean>
            <!-- reportgen view resolver handles xml -> pdf view translation -->
            <bean class="com.qualifacts.carelogic.spring.ReportgenViewResolver">
                <property name="delegate" ref="jspViewResolver"/>
            </bean>
            <!-- xml view resolver writes the raw xml to output -->
            <ref bean="xmlViewResolver"/>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
                <property name="objectMapper">
                    <bean class="com.qualifacts.carelogic.spring.CustomObjectMapper"/>
                </property>
            </bean>
        </list>
    </property>
</bean>

<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="true"/>
    <property name="useJaf" value="false"/>
    <property name="mediaTypes">
        <map>
            <entry key="html" value="text/html" />
            <entry key="pdf" value="application/pdf" />
            <entry key="xml" value="text/xml" />
            <entry key="json" value="application/json" /> 
        </map>
    </property>
</bean>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="2"/>
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
    <property name="redirectContextRelative" value="false"/>
    <property name="redirectHttp10Compatible" value="false" />
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="useSuffixPatternMatch" value="true"/>
</bean>


<tx:annotation-driven transaction-manager="transactionManager" />

我正在使用 spring 5. 我怎样才能使当我发送 pathvariable 参数 1017.json 时它只取值 1017?

/问题/changeProblem/1017.json

1017.json是一个String而不是一个数字,所以String是正确的。 基本上发送一个内部有数字的字符串并期望 Spring 不会抱怨它是糟糕的设计。 我们有数据类型是有原因的。 将参数作为String变量获取,然后删除所有不需要的非数字。 像这样:

yourParameter = yourParameter.replaceAll("[^\\d]",""); // replace all non digit characters with ""

// Convert your string into an integer:
int integerValueOfParameter = Integer.parse(yourParameter);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM