简体   繁体   中英

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

I am having a problem with either dot parameter in getting the request or displaying the json response in restful ws. If i am gonna fix the other, the other configuration may not work and vice versa. I am using the spring 4.0 and jackson 2.2.3.

I need these two to work. Like i need to make it sure that all the values that are passing to parameters are capture example the decimal point. http://servername.com/mysalary/50.90 i am just getting only the value of 50. and my result would be displayed as json format. Please see my configuration.

  1. here is my 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. Controller Class

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

Please help. I am taking too much time with this. :-(

In your request mapping, tell spring to take everything :

"/submitsalesorder/{address:.+}"

From SpringFramework documentation Web MVC

The @RequestMapping annotation supports the use of regular expressions in URI template variables. The syntax is {varName:regex} where the first part defines the variable name and the second - the regular expression.For example:

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

使用“ /submitsalesorder/{address:.+}”的建议解决方案解决了该问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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