0

I am using MyEclipse to generate a CRUD Application with REST service , the web CRUD application is well generated and working fine , but i want use also the REST service , the RestControler generated is like that :

@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());
    }

i tried to call this service using this url :

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

i use Postman to test this REST service , i could not get any response. what can be the problem ?

4
  • i tried it but not working Commented Jun 8, 2018 at 3:16
  • Try http://localhost:8080/JPO//News in postman, select Get method, and you'd better declare a new path for the get mapping. Commented Jun 8, 2018 at 3:20
  • i get a HTTP 406 error , it's something to do with Accept headers Commented Jun 8, 2018 at 3:22
  • i had to add jackson jars , now it's working , thx for your help guys Commented Jun 8, 2018 at 11:10

2 Answers 2

0

Using the port 8080 and the next implementation

@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);
    }
}

You must consume the service using:

http://localhost:8080/News

To get the below response:

{  "message" : "Testing Rest services!!!" }

When you run your application take a look at the log, it must say to you the path you need to consume, just add localhost:8080 at the beginning, below you can find my log for this example

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)
Sign up to request clarification or add additional context in comments.

2 Comments

I am not using spring boot, i use a spring mvc app
Springboot just avoid configuration tasks but it is almost the same, take a look at the logs when you run your application, on another hand try a @RestController instead
0

The parameter to @Controller annotation is to define a bean of given name NewsRestController to be used in spring context autowiring, its not to create a dispatcher URI mapping. You should be using @RequestMapping annotation like below to create URI path to be controlled by given controller.

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

Update: To your Http 406 error, make sure you have jackson jars in your classpath for json conversion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.