dimanche 22 février 2015

How to make sure that an object of a class is marshalled automatically by Spring boot if it is a Rest controller

I have a Spring boot app as follows which is responding to the query with a empty JSON "{}" in the body of the response.


according to my understanding the @RestController should autom aticall handle marshling of objects to JSON


Where am I going wrong?


My request : http://localhost:8080/name/sdhf


The response is :-


Status : 200 OK Show explanation Loading time: 353


Request headers


User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36 Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo Content-Type: application/json Accept: / DNT: 1 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.8


Response headers


Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Sun, 22 Feb 2015 21:44:15 GMT


ResponseBody:


{}


HelloWebAplication.scala



package helloKW
import org.springframework.http.converter.json
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.SpringApplication
import org.springframework.web.bind.annotation.{RequestMapping, RestController}
/**
* This object bootstraps Spring Boot web application.
* Via Gradle: gradle bootRun
*
*/
@EnableAutoConfiguration
object HelloWebApplication {

def main(args: Array[String]) {
SpringApplication.run(classOf[AxyzConfig]);
}
}


AxyzConfig.scala



package helloKW


import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.{ComponentScan, Configuration}
import org.springframework.http.{HttpStatus, ResponseEntity}
import org.springframework.web.bind.annotation._
import org.springframework._

/**
* This config class will trigger Spring @annotation scanning and auto configure Spring context.
*
*
*/
@RestController
@Configuration
@EnableAutoConfiguration
@ComponentScan
class AxyzConfig {
var m : Moderator = new Moderator(10,"shgd","fsd","ifge")
var x =10

@RequestMapping(method = Array {RequestMethod.POST},value=Array{"/name/{name}"})
@ResponseBody
def returnParamName(@PathVariable name:String) = new Moderator(45,"sjfg","ksgfs","kurreuk")

}


Moderator.scala



package helloKW


class Moderator (){
var id = 10
var name ="jdgsjdhg"
var email ="sdkgsd"
var password ="sdkfjhsdfk"

def this(a:Int,b:String,c:String,d:String){
this()
this.id = a
this.email = b
this.name = c
this.password = d
}


}

Aucun commentaire:

Enregistrer un commentaire