使用MyEclipse生成Spring REST Web服务

时间:2018-06-08 02:57:55

标签: java spring rest web-services myeclipse

我正在使用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());
    }

我尝试使用此网址调用此服务:

http://localhost:8080/JPO/NewsRestController/News

我使用Postman来测试这个REST服务,我无法得到任何回复。 可能是什么问题?

2 个答案:

答案 0 :(得分:0)

@Controller注释的参数是定义要在spring上下文自动装配中使用的给定名称NewsRestController的bean,而不是创建调度程序URI映射。您应该使用如下所示的@RequestMapping注释来创建由给定控制器控制的URI路径。

@Controller("NewsRestController")
@RequestMapping("/NewsRestController")

更新:对于你的Http 406错误,请确保你的类路径中有jackson jar用于json转换。

答案 1 :(得分:0)

使用端口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)