-2

Can somebody tell me how to configure @RestController?

I do this :

@RestController
@EnableAutoConfiguration
public class Application {

    @RequestMapping("/test.htm")
    @ResponseBody
    String home() {
       return "Hello Worlds!";
    }

    public static void main(String[] args) throws Exception {
       SpringApplication.run(Application.class, args);
    }
}

@Controller
public class MusicControler {

    class Test{
        String name;
        int age;
    }

    @RequestMapping(value = "/MyController")
    public Test MyController() {
        Test test = new Test();
        test.name = "zl.shi";
        test.age = 16;
        return test;
    }
}

When I request /test.htm, it is ok but I get response 404 for /testController.htm. Can someone help me about it?

IAmInPLS
  • 4,051
  • 4
  • 24
  • 57
serio.shi
  • 1
  • 2
  • 1
    You haven't defined a mapping for `/testController.htm` therefore it can't be found. – Patrick Apr 06 '16 at 07:22
  • you can use @ RestController in place of @ Controller annotation . @ RestController implies that the Whatever object which you are returning from method will be send as as response body to client who is calling it . @ RestController = @ Contoller +@ ResponseBody . – kedar kamthe Apr 06 '16 at 07:27
  • You don't have any mapping to `/testController.htm`, how can it be found? Change `/MyController` to `/testController.htm`. – dambros Apr 06 '16 at 07:42

2 Answers2

0

use following code to create rest controller

@RestController
@RequestMapping("/service/")  
public class Application {

@RequestMapping(value = "/getmemberdetail/{id}/{info}", method = RequestMethod.GET,  produces = { "application/json" })
    public ResponseEntity<String> getuserdetail(@PathVariable int portalType,@PathVariable("id") int id,@PathVariable("info") String info) throws JsonProcessingException, ParseException{}

}
vikash
  • 129
  • 2
  • 11
0

if you want to know how to use it you should read about it Difference between spring @Controller and @RestController annotation when you make starter-project spring you should make another class where you are going to put you controller don't forget RestController annotation or the controller annotation ( as best practice you shouldn't use the starter class of spring boot ) i hope this helps you

ps don't tag spring-mvc and spring-boot it's not the same thing

Community
  • 1
  • 1
Kamel Mili
  • 1,374
  • 3
  • 19
  • 43