Need application/json from RESTful Roo application
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 domai开发者_如何学Cn objects as application/json through a WS?
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);
}
精彩评论