vendredi 20 mars 2015

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://ift.tt/1Fc8lXI 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://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
xmlns:util="http://ift.tt/OGfeTW" xmlns:mvc="http://ift.tt/1bHqwjR"
xsi:schemaLocation="http://ift.tt/1bHqwjR http://ift.tt/JWpJWM
http://ift.tt/GArMu6 http://ift.tt/18sW2ax
http://ift.tt/OGfeTW http://ift.tt/1aj0W5e
http://ift.tt/GArMu7 http://ift.tt/1bGeTcI">

<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. :-(


Aucun commentaire:

Enregistrer un commentaire