如何拆分类以使用不同的@Controller

时间:2019-07-13 15:01:53

标签: java spring spring-boot spring-annotations

我正在学习弹簧靴,并开发了belwo简单示例。我有GreetingApplicationModelObject和两个用@Controller注释的类。 我使用的端口是

8085

运行代码时,我收到以下发布的错误消息。 请让我知道如何解决。

SpringBootApplication

@SpringBootApplication
@RestController
public class GreetingApplicationModelObject {

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

Controller1

@Controller
public class GreetingWithParams {

@RequestMapping("/greetingWithParams")
public Greeting Greeting(@RequestParam(value = "val", defaultValue = "no_val_from_front-end") String val) {
    return new Greeting(getRandomValue(), val);
}

public int getRandomValue() {
    Random r = new Random();
    return r.nextInt(1000);
}
}

Controller2

@Controller
public class GreetingText {

@RequestMapping("/")
public String GreetingText() {
    return "Frontend Greeting";
}
}

错误

-------------------------------------------------------------------------------
Test set: com.example.greeting.GreetingApplicationTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.279 s <<< FAILURE! - in com.example.greeting.GreetingApplicationTests
initializationError(com.example.greeting.GreetingApplicationTests)  Time elapsed: 0.004 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

0 个答案:

没有答案
相关问题