我在 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”。所以加号(+)没有被解码。需要做什么才能在服务器上获得加号 (+)?任何帮助表示赞赏。