spring mvc decoding +正在被空间取代

时间:2012-12-05 08:26:29

标签: iphone rest spring-mvc

我在Spring MVC模式中遇到问题。当我用带编码值的iPhone应用程序调用Web服务时,解码没有正常进行。不确定是什么问题。 +符号被替换为空格“”。 下面是我的web.xml

<?xml version="1.0" encoding="UTF-8"?><br>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br>
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"<br>
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"<br>
id="WebApp_ID" version="3.0"><br>
<display-name>APPLICATIONNAME</display-name><br>
<filter><br>
<filter-name>encoding-filter</filter-name><br>
<filter-class>org.springframework.web.filter.CharacterEnco dingFilter</filter-class><br>
<init-param><br>
<param-name>encoding</param-name><br>
<param-value>UTF-8</param-value><br>
</init-param><br>
<init-param><br>
<param-name>forceEncoding</param-name><br>
<param-value>true</param-value><br>
</init-param><br>
</filter><br>
<filter-mapping><br>
<filter-name>encoding-filter</filter-name><br>
<url-pattern>/rest/*</url-pattern><br>
</filter-mapping><br>
<servlet><br>
<servlet-name>spring</servlet-name><br>
<servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class><br>
<load-on-startup>1</load-on-startup><br>
</servlet><br>
<servlet-mapping><br>
<servlet-name>spring</servlet-name><br>
<url-pattern>/rest/*</url-pattern><br>
</servlet-mapping><br>
<welcome-file-list><br>
<welcome-file>index.jsp</welcome-file><br>
</welcome-file-list><br>
</web-app><br>

我的控制器类

@Controller
public class LoginController {

@Autowired
private LoginServiceImpl loginService;

@RequestMapping(value = "/getUserDetails", method = RequestMethod.POST)
public @ResponseBody
LoginData getUserDetails(@ModelAttribute LoginDetails loginDetails) {
LOGGER.debug("Session ID = " + loginDetails.getSessionID());
}

来自iPhone APP的示例输入请求

NSURLResponse *response = nil;
NSError *error = nil;
NSString *urlString = @"http://localhost:8080/rest/getUserDetails";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:urlRequest
                                                             delegate:self];
[urlRequest setHTTPMethod:@"POST"];
NSString *postString = [NSString stringWithFormat:@"sessionID=%@",@"ABCD+/EFGH"];
NSData *a=[postString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"encoded %@",a);
[urlRequest setHTTPBody:a];
    [connection start];
NSString *returnString = [[NSString alloc] initWithData:[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error] encoding:NSUTF8StringEncoding];

NSLog(@" %@ ",returnString);
NSLog(@"%@",error);

在服务器端而不是“ABCD + / EFGH”我得到“ABCD / EFGH”。所以加号(+)没有得到解码。 要在服务器上获得加号(+)需要做什么?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

这是一种预期的行为。

application/x-www-form-urlencoded form(用于在POST中发送表单数据)空间编码为+,而真实+应以百分比编码形式表示(%2B

我认为最好使用某种内置的application/x-www-form-urlencoded编码例程(如果在iPhone上可用),而不是手动替换。