如何配置@RestController?

时间:2016-04-06 07:14:11

标签: java spring spring-mvc spring-boot

有人可以告诉我如何配置@RestController吗?

我这样做:

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

当我请求/test.htm时,没关系,但我得到/testController.htm的回复404。有人能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

使用以下代码创建休息控制器

@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{}

}

答案 1 :(得分:0)

如果您想知道如何使用它,您应该阅读它 Difference between spring @Controller and @RestController annotation 当你制作初学者项目的春天时,你应该创建另一个课程,你要让你的控制器不要忘记RestController注释或控制器注释(最好的做法是你不应该使用spring boot类的初学者)我希望这可以帮助你

ps不要标记spring-mvc和spring-boot它不是一回事