作为一种爱好,我自己学习 spring。 观察问题。
这是我正在使用的演示休息 Controller 。
@RestController
class DemoHelloService
{
@GetMapping("/hello")
public String hello(@RequestParam String name)
{
String Name = new String("Hello "+name);
return Name;
}
}
URL localhost:8080/hello?name=测试观察响应为“Hello Test”。 这里的问题是在字符串名称中,我在“Hello”旁边有尾随空格。但这在服务响应中看不到。
任何需要对服务中的接收尾随空格进行更改的帮助。
最佳答案
试试这个:
String Name = new String("Hello "+name);
正如您在这里看到的:What's the difference between " " and " "?非破坏规范不会在 html 中折叠,而空间会折叠。
当然,在任何真正的 api 中,您都不想返回 html 字符。最好在客户端返回名称(在您的示例中它不适用)和格式。
关于java - Spring Web服务删除响应字符串中的多个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701830/