簡體   English   中英

如何在restful webservice中處理request參數中的點並顯示json響應

[英]How to deal with the dot in the request parameter and display json response in restful webservice

我在獲取請求或在平穩的ws中顯示json響應時遇到了dot參數的問題。 如果我要解決其他問題,則其他配置可能不起作用,反之亦然。 我正在使用spring 4.0和jackson 2.2.3。

我需要這兩個來工作。 就像我需要確保傳遞給參數的所有值都是捕獲示例的小數點。 http://servername.com/mysalary/50.90我只是得到50的值,我的結果將顯示為json格式。 請查看我的配置。

  1. 這是我的applicationContext.xml

 <?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> 

  1. JsonObjectMapper

 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); } } 

  1. 控制器類

 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 **/ } } 

  1. 我的pom.xml

 <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> 

請幫忙。 我花了太多時間。 :-(

在您的請求映射中,告訴spring接受所有操作:

"/submitsalesorder/{address:.+}"

從SpringFramework文檔Web MVC

@RequestMapping批注支持在URI模板變量中使用正則表達式。 語法為{varName:regex},其中第一部分定義變量名,第二部分定義正則表達式。例如:

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

使用“ /submitsalesorder/{address:.+}”的建議解決方案解決了該問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM