简体   繁体   English

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

[英]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. 我在获取请求或在平稳的ws中显示json响应时遇到了dot参数的问题。 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. 我正在使用spring 4.0和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. http://servername.com/mysalary/50.90我只是得到50的值,我的结果将显示为json格式。 Please see my configuration. 请查看我的配置。

  1. here is my applicationContext.xml 这是我的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 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 我的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 : 在您的请求映射中,告诉spring接受所有操作:

"/submitsalesorder/{address:.+}"

From SpringFramework documentation Web MVC 从SpringFramework文档Web MVC

The @RequestMapping annotation supports the use of regular expressions in URI template variables. @RequestMapping批注支持在URI模板变量中使用正则表达式。 The syntax is {varName:regex} where the first part defines the variable name and the second - the regular expression.For example: 语法为{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