我的简单 Spring Web 服务有什么问题?
what's wrong with my simple Spring web service?
我构建了一个简单的 Spring Web 应用程序。我有这个带有@RequestMapping 的简单@Controller,但是当我 运行 它时,我无法点击 URL:
http://localhost:8080/labutil/all
我做错了什么?
package com.mycompany.ion.labutil.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.nokia.ion.labutil.service.LabService;
@Controller
public class LabController {
@Autowired
private LabService labService;
@RequestMapping(value = "/all", method = RequestMethod.GET)
public String getAll() throws Exception {
List<String> list = labService.getAll();
// build fake little json formatted data
StringBuffer sb = new StringBuffer("{");
for (String s : list) {
sb.append("{ "+s+" }, ");
}
sb.append("}");
return sb.toString();
}
}
您必须将您的控制器注释为@RestController 或将@ResponseBody 注释添加到您的方法。通过这种方式,您告诉 Spring 此方法 return 将对象作为 HTTP 正文响应。 @RestController 是一个方便的注解,同时注解了 @Controller 和 @ResponseBody 注解。
为什么要使用这个注解的答案。
@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public String getAll() throws Exception {
List<String> list = labService.getAll();
// build fake little json formatted data
StringBuffer sb = new StringBuffer("{");
for (String s : list) {
sb.append("{ "+s+" }, ");
}
sb.append("}");
return sb.toString();
}
另一方面,您应该 return 一个对象,而不是解析的字符串 Json,添加一些 Json 库,如 Jackson 或 Gson,并使用相应的库配置视图查看实现。
我构建了一个简单的 Spring Web 应用程序。我有这个带有@RequestMapping 的简单@Controller,但是当我 运行 它时,我无法点击 URL:
http://localhost:8080/labutil/all
我做错了什么?
package com.mycompany.ion.labutil.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.nokia.ion.labutil.service.LabService;
@Controller
public class LabController {
@Autowired
private LabService labService;
@RequestMapping(value = "/all", method = RequestMethod.GET)
public String getAll() throws Exception {
List<String> list = labService.getAll();
// build fake little json formatted data
StringBuffer sb = new StringBuffer("{");
for (String s : list) {
sb.append("{ "+s+" }, ");
}
sb.append("}");
return sb.toString();
}
}
您必须将您的控制器注释为@RestController 或将@ResponseBody 注释添加到您的方法。通过这种方式,您告诉 Spring 此方法 return 将对象作为 HTTP 正文响应。 @RestController 是一个方便的注解,同时注解了 @Controller 和 @ResponseBody 注解。
@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public String getAll() throws Exception {
List<String> list = labService.getAll();
// build fake little json formatted data
StringBuffer sb = new StringBuffer("{");
for (String s : list) {
sb.append("{ "+s+" }, ");
}
sb.append("}");
return sb.toString();
}
另一方面,您应该 return 一个对象,而不是解析的字符串 Json,添加一些 Json 库,如 Jackson 或 Gson,并使用相应的库配置视图查看实现。