2

I've created a basic RESTful Roo application using the following Roo-script (Roo 1.1.5).

project --topLevelPackage com.roorest
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY 
entity --class ~.domain.MyClass
field string --fieldName String1
web mvc setup
web mvc all --package ~.web
json all

When I access the RESTful WS asking for application/json the WS spits out a valid json body, however the content type is set to application/text (which makes perfect sense if one looks at the generated (aj) controller code ticking in the background).

Unfortunately, I need to have the WS return a content type of application/json. I've tried to push in the necessary methods from the json-controllers, however this seems 1) cumbersome, 2) not really working (I'm getting a LOT of errors with the pushed in source).

Can one force the WS return application/json on a general basis? For instance, is it possible to combine ContentNegotiatingViewResolver with the roo generated aj controllers? (And why does the roo generated code explicitly set application/text as it's content type in the first place? Is hacking the roo JSON addon a viable solution?)

I guess what I'm really asking is this: what do you think is the best way to make a roo scaffolded application return domain objects as application/json through a WS?

conciliator
  • 5,834
  • 6
  • 38
  • 62

1 Answers1

2

Did you solve the problem, because I just having the same one...?

Okay, I do have one solution: Add the methods to your controller and do not let the AOP Framework add them:

@RequestMapping(headers = "Accept=application/json")
@ResponseBody
public ResponseEntity<String> listJson() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", "application/json; charset=utf-8"); //was app/text
    return new ResponseEntity<String>(Customer.toJsonArray(Customer
            .findAllCustomers()), headers, HttpStatus.OK);
}
Alexander
  • 7,028
  • 8
  • 42
  • 73
  • yeah ... that's what I ended up doing myself. Really unsatisfactory, though. Right now I'm working on the client side, so haven't given this much thought lately. I just can't understand why they keep adding app/text. It's not in conformance with the offical docs http://stackoverflow.com/questions/477816/the-right-json-content-type. – conciliator Oct 06 '11 at 11:40
  • @Alexander. Did you put this in the Controller_Roo_Controller_Json.aj file? I thought we are not suppose to modify this file? – okysabeni Oct 17 '11 at 16:12
  • No just overwrite the listJson method in the controller. The AOP framework with the ITD does recognize, that this method already exists and removes the aspect from the *.aj file. – Alexander Oct 18 '11 at 05:42