3

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
  }


}

1 Answer 1

2

If your using Jackson as your default serializer it is expected a Moderator to be a java bean with getters and setters. As there are no getters and setters the object is empty.

You have two main options.

Option 1
Class with BeanProperty annotation. I'd prefer a case class here but you're using a regular class which should still be fine. So each field will be something like:

@BeanProperty
var name ="jdgsjdhg"

Option 2
Use Case Classes and register Jackson Scala Module with the object mapper. See here. This to me seems the better option but requires a little extra setup up front.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.