官术网_书友最值得收藏!

Modifying our microservice

Now, let's navigate in our code so we can edit it for adding some changes.

  1. In the Project window that is on the top-left side of the screen, expand src | main | Kotlin to view our source files
  2. Then we can open our packages until we see our application: Chapter2Application.kt
package com.microservices.chapter2

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class Chapter2Application

fun main(args: Array<String>) {
runApplication<Chapter2Application>(*args)
}

This code will create and run a Spring Boot application, that will launch a microservice listening on port 8080.

We can see that, in the gutter area of the Edit Window, there is a Kotlin symbol in the same line that our main function is. If we click on it, we can run this function and it will launch our microservice and a Run window will appear that allows us to view the log, stop, or restart the process.

Now, let's modify the example code:

package com.microservices.chapter2

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod

import org.springframework.web.bind.annotation.ResponseBody

@SpringBootApplication
class Chapter2Application

@Controller
class FirstController {

@RequestMapping(value = "/user", method = arrayOf(RequestMethod.GET))
@ResponseBody
fun hello() = "hello world"
}


fun main(args: Array<String>) {
runApplication<Chapter2Application>(*args)
}

If we run the application now and go to the browser to the URL http://localhost:8080/user/, we will see the following:

hello world

This small change adds a controller, basically a class that will handle requests to our microservice, and then we add a mapping for a particular path, in this example /user, then it will output hello world to any request.

Now, let's create a new class, in the project window, right-click on our package, and then choose New | Kotlin File / Class.

Create files in the project window

A new window will appear asking us for the name, let's type ExampleService and choose Class in the Kind dropdown.

naming the new file

Then, we will add this code:

package com.microservices.chapter2

import org.springframework.stereotype.Service

@Service
class ExampleService {
fun getHello(name : String) = "hello $name"
}

Finally, we can modify our controller to use this newly created service:

package com.microservices.chapter2

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Controller
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.ResponseBody

@SpringBootApplication
class Chapter2Application

@Controller
class FirstController(val exampleService: ExampleService) {
@RequestMapping(value = "/user/{name}", method = arrayOf(RequestMethod.GET))
@ResponseBody
fun hello(@PathVariable name: String) = exampleService.getHello(name)
}

fun main(args: Array<String>) {
runApplication<Chapter2Application>(*args)
}

We set an exampleService instance as an attribute of our controller; then, to create our output, we will get the name of the user that we greet as a path variable and we will call the service to get the result.

Executing the service and going to the URL http://localhost:8080/user/Kotlin, will output the following:

hello Kotlin
We will get more understanding of many of the elements in Chapter 3, Creating RESTful services.
主站蜘蛛池模板: 唐海县| 望谟县| 扎兰屯市| 日喀则市| 马关县| 定南县| 扎鲁特旗| 贵阳市| 洛隆县| 时尚| 孙吴县| 福鼎市| 万盛区| 九龙县| 建德市| 布拖县| 五常市| 拜城县| 尼玛县| 绩溪县| 永城市| 铜陵市| 若羌县| 扬州市| 福海县| 垫江县| 漳州市| 安阳市| 嘉义市| 桓台县| 扎兰屯市| 黄石市| 烟台市| 麻江县| 鄂尔多斯市| 宿松县| 仪陇县| 大宁县| 伊宁县| 花垣县| 资源县|