【问题标题】:Generating Spring REST webservice using MyEclipse使用 MyEclipse 生成 Spring REST Web 服务
【发布时间】:2018-06-08 02:57:54
【问题描述】:

我正在使用 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

我使用 Postman 来测试这个 REST 服务,我没有得到任何响应。 可能是什么问题?

【问题讨论】:

  • 我试过了,但没有用
  • 在postman中试试http://localhost:8080/JPO//News,选择Get方法,你最好声明一个新的get映射路径。
  • 我收到 HTTP 406 错误,这与 Accept 标头有关
  • 我不得不添加杰克逊罐子,现在它正在工作,谢谢你们的帮助

标签: java spring rest web-services myeclipse


【解决方案1】:

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

【讨论】:

  • 我没有使用spring boot,我使用的是spring mvc app
  • Springboot 只是避免了配置任务,但几乎相同,在运行应用程序时查看日志,另一方面尝试使用 @RestController 代替
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2019-02-25
    • 2015-10-21
    • 2015-08-04
    • 2014-01-15
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多