我正在使用MyEclipse生成一个带有REST服务的CRUD应用程序,Web CRUD应用程序生成良好并且工作正常,但是我也想使用REST服务,生成的RestControler是这样的:
@Controller("NewsRestController")
public class NewsRestController {
/**
* DAO injected by Spring that manages News entities
*
*/
@Autowired
private NewsDAO newsDAO;
/**
* Service injected by Spring that provides CRUD operations for News entities
*
*/
@Autowired
private NewsService newsService;
/**
* Create a new News entity
*
*/
@RequestMapping(value = "/News", method = RequestMethod.POST)
@ResponseBody
public News newNews(@RequestBody News news) {
newsService.saveNews(news);
return newsDAO.findNewsByPrimaryKey(news.getId());
}
/**
* Show all News entities
*
*/
@RequestMapping(value = "/News", method = RequestMethod.GET)
@ResponseBody
public List<News> listNewss() {
return new java.util.ArrayList<News>(newsService.loadNewss());
}
我尝试使用此 URL 调用此服务:
http://localhost:8080/JPO/NewsRestController/News
我使用邮递员来测试这个REST服务,我没有得到任何响应。 可能是什么问题?
使用端口 8080 和下一个实现
@SpringBootApplication
@ComponentScan
@RestController
public class ApplicationStarter {
@RequestMapping(value = "/News", method = RequestMethod.GET, produces="application/json")
public ResponseEntity<String> newNews() {
return ResponseEntity.ok("{ "message" : "Testing Rest services!!!" }");
}
@RequestMapping
public static void main(String[] args) {
SpringApplication.run(ApplicationStarter.class, args);
}
}
您必须使用以下方法使用该服务:
http://localhost:8080/News
要获取以下响应,请执行以下操作:
{ "message" : "Testing Rest services!!!" }
当你运行你的应用程序时,看看日志,它必须告诉你你需要使用的路径,只需在开头添加localhost:8080,下面你可以找到我这个例子的日志
2018-06-07 23:00:37.030 INFO 15552 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/News],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<java.lang.String> org.tocode.hystrix.ApplicationStarter.newNews()
2018-06-07 23:00:37.034 INFO 15552 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/messages],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<java.util.List<java.lang.String>> org.tocode.hystrix.controller.HystrixController.getMessages()
2018-06-07 23:00:37.036 INFO 15552 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/uniq-messages/],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> org.tocode.hystrix.controller.HystrixController.getUniqMessage()
2018-06-07 23:00:37.137 INFO 15552 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/messages/{messageId}],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<java.lang.String> org.tocode.hystrix.controller.HystrixController.getMessageById(int)
2018-06-07 23:00:37.140 INFO 15552 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/myapp/user/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.lang.String> org.tocode.hystrix.controller.User.getId(int)
@Controller
注释的参数是定义一个给定名称的 bean,NewsRestController
用于 Spring 上下文自动布线,它不是创建调度程序 URI 映射。您应该使用如下所示@RequestMapping
注释来创建由给定控制器控制的 URI 路径。
@Controller("NewsRestController")
@RequestMapping("/NewsRestController")
更新:对于 Http 406 错误,请确保在类路径中有用于 json 转换的 jackson jar。