SpringBoot to the Micronaut® framework - Controller annotations

SpringBoot and Micronaut applications ease the creation of routes with similar annotations.

SpringBoot:

package scorekeep;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.Map;

@RestController
@RequestMapping("/api/score")
public class ScoreController {

    @RequestMapping(value = "/{gameId}", method = RequestMethod.GET)
    public Map<String, Object> index(@PathVariable String gameId) {        
        return Collections.singletonMap("messages", "Hello World");
    }
}

Micronaut® framework:

package scorekeep;

import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;

import java.util.Collections;
import java.util.Map;

@Controller("/api/score")
public class ScoreController {

    @Get( "/{gameId}")
    public Map<String, Object> index(@PathVariable String gameId) {
        return Collections.singletonMap("messages", "Hello World");
    }
}

I like to include the @PathVariable annotation to convey the parameter context. However, it is not required.

It is easy to migrate Spring Boot request mapping annotations to Micronaut annotations. Micronaut® framework includes annotations for the HTTP verbs @Get, @Post, @Delete, @Put... They are succinct. Succinct code is good code.

My next events:
🗓 May 27 09:00 AWS Dublin - Micronaut and AWS Lambda
🗓 Jul 20 09:00 Workshop Security in the Micronaut Framework
🗓 Sep 20 09:00 JCON Online 2022 - Getting Started with the Micronaut Framework
🗓 Sep 23 09:00 JCON Online 2022 - Micronaut® Framework and Microstream Java-native persistence engine

Micronaut® is a registered trademark of Object Computing, Inc. Use is for referential purposes and does not imply any endorsement or affiliation with any third-party product. Unauthorized use is strictly prohibited.

Tags: #micronaut #springboot
Apr 2021, 27.