如何处理请求参数中的点并在restful webservice中显示json响应

时间:2015-03-20 08:10:51

标签: java json spring rest jackson

我在获取请求时使用dot参数或在restful ws中显示json响应时遇到问题。如果我要修复另一个,其他配置可能无法正常工作,反之亦然。我使用的是弹簧4.0和杰克逊2.2.3。

我需要这两个才能工作。就像我需要确保传递给参数的所有值都是捕获示例小数点。 http://servername.com/mysalary/50.90我只得到50的值。我的结果将显示为json格式。请参阅我的配置。

  1. 这是我的applicationContext.xml
  2. 
    
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
      
    	<context:component-scan base-package="com.wom.api.controller" />
    	<!-- this is to allow getting the dot in the request. this one is not working fine -->	  
    	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping">
            <property name="useSuffixPatternMatch" value="false"></property>
            <property name="useTrailingSlashMatch" value="false"></property>
    	</bean>
    	
    	
    	<mvc:annotation-driven >
        <mvc:message-converters>
        	<!-- this will allow the display of json response and running just fine -->
    		<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.wom.api.config.JasonObjectMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    	</mvc:annotation-driven>
    	
    	<!-- 	Enable the images, css, an etc. -->	
    	<mvc:resources mapping="/resources/**" location="/resources/" />
    		
    	<!-- Load Hibernate related configuration -->
    	<import resource="hibernate-context.xml" />
    	<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="resources" />
    	</bean>
    </beans>
    &#13;
    &#13;
    &#13;

    1. JsonObjectMapper
    2. &#13;
      &#13;
      import com.fasterxml.jackson.annotation.JsonAutoDetect;
      import com.fasterxml.jackson.annotation.PropertyAccessor;
      import com.fasterxml.jackson.databind.ObjectMapper;
      import com.fasterxml.jackson.databind.SerializationFeature;
      
      public class JasonObjectMapper extends ObjectMapper{
      
      	private static final long serialVersionUID = 1L;
      
      	public JasonObjectMapper() {
      		System.out.println("Pass Here");
      	    this.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
      	            .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.ANY)
      	            .setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
      	            .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
      	            .setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);
      
      	    this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
      	    }
      }
      &#13;
      &#13;
      &#13;

      1. 控制器类
      2. &#13;
        &#13;
        public class SalesOrderController {
        
        	@Autowired
        	SalesOrderService salesorderService;
        	
        	static final Logger logger = Logger.getLogger(SalesOrderController.class);
        	
        	/** GET Method **/
        	@RequestMapping(value = "/submitsalesorder/{address}", method=RequestMethod.GET, produces = "Application/json")
        	public @ResponseBody JSONArray submitSalesOrderGET(@PathVariable("address") String address) throws Exception{
        		
             /** my code goes here **/
        		
        	}
        }
        &#13;
        &#13;
        &#13;

        1. 我的pom.xml
        2. &#13;
          &#13;
          <properties>
          	<spring.version>4.0.5.RELEASE</spring.version>
          	<hibernate.version>4.3.5.Final</hibernate.version>
          	<log4j.version>1.2.17</log4j.version>
            </properties>
            <!-- Spring dependency -->    
            <dependency>
          		<groupId>org.springframework</groupId>
          		<artifactId>spring-orm</artifactId>
          		<version>${spring.version}</version>
          	</dependency>
          	<dependency>
          		<groupId>org.springframework</groupId>
          		<artifactId>spring-jdbc</artifactId>
          		<version>${spring.version}</version>
          	</dependency>
          	
          	<dependency>
          		<groupId>org.springframework</groupId>
          		<artifactId>spring-web</artifactId>
          		<version>${spring.version}</version>
          	</dependency>
          	
          	<dependency>
          		<groupId>org.springframework</groupId>
          	  	<artifactId>spring-context</artifactId>
          	    <version>${spring.version}</version>
          	</dependency>
          	
          	<dependency>
          		<groupId>org.springframework</groupId>
          		<artifactId>spring-webmvc</artifactId>
          		<version>${spring.version}</version>
          	</dependency>
          
          <!-- CodeJackson -->
          	
          	<dependency>
          	    <groupId>com.fasterxml.jackson.core</groupId>
          	    <artifactId>jackson-core</artifactId>
          	    <version>2.2.3</version>
          	</dependency>
          	<dependency>
          	    <groupId>com.fasterxml.jackson.core</groupId>
          	    <artifactId>jackson-databind</artifactId>
          	    <version>2.2.3</version>
          	</dependency>
          	<dependency>
          	    <groupId>com.fasterxml.jackson.core</groupId>
          	    <artifactId>jackson-annotations</artifactId>
          	    <version>2.2.3</version>
          	</dependency>
          
          	<dependency>
          	    <groupId>org.codehaus.jackson</groupId>
          	    <artifactId>jackson-core-asl</artifactId>
          	    <version>1.9.13</version>
          	</dependency>
          	
          	<dependency>
          	    <groupId>org.codehaus.jackson</groupId>
          	    <artifactId>jackson-mapper-asl</artifactId>
          	    <version>1.9.13</version>
          	</dependency>
          &#13;
          &#13;
          &#13;

          请帮忙。我花了太多时间。 : - (

2 个答案:

答案 0 :(得分:0)

在你的请求映射中,告诉spring采取一切:

"/submitsalesorder/{address:.+}"

来自SpringFramework文档Web MVC

  

@RequestMapping注释支持使用正则表达式   在URI模板变量中。语法是{varName:regex},其中   第一部分定义变量名称,第二部分定义常规   表达式。例如:

@RequestMapping("/spring-web/{symbolicName:[a-z-]}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]}")
    public void handle(@PathVariable String version, @PathVariable String extension) {
        // ...
    }
}

答案 1 :(得分:0)

使用“/ submitsalesorder / {address:。+}”

的建议解决方案解决了这个问题