两种方法 ,一种是使用json4s库转换成字符串,然后用注解produces = Array("application/json;charset=UTF-8")标明返回的content-type类型,
另外一种方法是将case class的属性注解为BeanProperty,并且在Controller方法前面加上@ResponseBody
import org.json4s.DefaultFormats
import org.springframework.web.bind.annotation.{GetMapping, ResponseBody, RestController}
import org.json4s._
import org.json4s.jackson.Serialization.{read, write}
import scala.beans.BeanProperty
@RestController
class MainContainer {
implicit val formats = DefaultFormats
@GetMapping(value=Array("/find"), produces = Array("application/json;charset=UTF-8"))
def find():String={
write(UrlRecord("www.baidu.com","/index.html",1,2))
}
@GetMapping(value=Array("/findtask"))
@ResponseBody
def findTask():UrlRecord={
UrlRecord("www.baidu.com","/index.html",1,2)
}
}
case class UrlRecord(@BeanProperty host:String,@BeanProperty path:String,@BeanProperty flux:Long,@BeanProperty request:Long)