如何拆分类以使用服务器@Controller

时间:2019-07-13 14:04:25

标签: java spring spring-boot spring-annotations

我正在学习Spring Boot,并开发了以下简单示例。我想使用@Controller将类注释为Controller。此类具有构造函数,我想访问GreetingFromDeuController,如下所示:

http://localhost:8080:/GreetingFromDeuController?str = "hi"

我收到的错误是

@RequestMapping is not applicable on a constructor

请让我知道如何解决。

代码

@Controller
@RequestMapping("/GreetingFromDeuController")
public class GreetingFromDeuController {

private String str;

@RequestMapping("/GreetingFrom/deu")
GreetingFromDeuController(@RequestParam(value = "str") String str) {
    this.str = str;
}

@RequestMapping("/GreetingFromDeuController")
public String getGreetingFromDeu() {
    return this.str;
}   
}

4 个答案:

答案 0 :(得分:1)

首先,构造函数在到达URL之前就进行了初始化。因此,您需要进行设计或告诉我您的业务需求,我将尽力为您提供解决方案。我的重构代码解决方案将帮助您分两步实现。第一次点击POST方法将对设置变量起作用,然后再次点击GET方法将返回该设置值。

我们可以重构如下代码。它将说明在方法和类上使用RequestMapping。

考虑到我们必须编写两个API,一个用于读取,一个用于编写。

URL:

1. POST http://localhost:8080/example/greetings (in request body send {str:'hi'})
2. GET  http://localhost:8080/example/greetings

 @Controller
 @RequestMapping("/example")
 public class GreetingFromDeuController {

  private String str;

  @RequestMapping(value="/greetings" , method = RequestMethod.POST)
  public void setGreetingFromDeu(@RequestBody(value = "str") String str) 
  {
    this.str = str;
  }

  @RequestMapping(value="/greetings" , method = RequestMethod.GET)
  public String getGreetingFromDeu() 
  {
   return this.str;
  }   
}

答案 1 :(得分:0)

就我而言,@RequestMapping并不适用于构造函数。它应该用于注释方法或类。负责处理请求的方法。

答案 2 :(得分:0)

@RequestMapping文档说:

  

在请求处理中将Web请求映射到方法中的注释   具有灵活方法签名的类。

然后,如果您想初始化变量或使用多种方法进行的操作,您将无法做到这一点:

1。-使用@PostConstruct

@PostContruct
public void init() {
   this.str = "Anything";
}

2.-使用简单的请求仅设置任何内容

@RequestMapping(value="/refresh/anythings", method = RequestMethod.PUT)
public void refresh(@RequestBody(value = "str") String str) {
    this.str = str;
}   

3。-使用@Value

在application.properties / application.yaml中

properties.str = anything    

在控制器中

@Value("${properties.str:default}") // by default str is "default"
public String str;

@RequestMapping(value="/greetings" , method = RequestMethod.GET)
public String getGreetingFromDeu() {
    return this.str;
}       

答案 3 :(得分:0)

@RequestMapping应该用于 map request with endPoint 。可以用作类级别和方法级别。

您可以使用@RestController (从@Controller see difference开始改进)

Spring Boot的理想流程是 Controller -> Service -> Repository

Controller -> maps request with endPoint and return response
Service -> perform business logic
Repository -> Handle database operation

示例

@RestController
    @RequestMapping("/api")
    public class GreetingController {

    @Autowired GreetinService greetingService;

    // Request http://localhost:8080/api/GreetingFrom
    @GetMapping("/GreetingFrom")
    public ResponseEntity<String> GreetingRequestParam(@RequestParam(value = "name") String name) {
        greetingService.performBusinessLogic(name);
        return new ResponseEntity<String>("Greetings from "+name,HttpStatus.OK);
    }

    // Request http://localhost:8080/api/GreetingFrom/user2121
    @GetMapping("/GreetingFrom/{name}")
    public ResponseEntity<String> GreetingPathVariable(@PathVariable(value = "name") String name) {
        return new ResponseEntity<String>("Greetings from "+name,HttpStatus.OK);
    }
}
相关问题