How to obtain JSON (or XML) response from Grails? - rest

I have 1 domain class, 1 controller, 1 URL mapping (see below). I want to send request, and receive JSON response.
But currently my request is correctly mapped to the correspondent controller method, the method is successfully executed, and then I get an error with message that jsp-file not available.
How to explain Grails, that I don't need no jsp-files: I want to receive/parse JSON requests, and send JSON responses right from my controllers?
class Brand {
String name
String description
String logoImageURL
static constraints = {
name(blank: false)
}
}
---------------------------
class UrlMappings {
static mappings = {
"/brands"(controller: "brand", parseRequest: true, action: "list")
}
}
---------------------------
import grails.converters.JSON
class BrandController {
def list = {
return Brand.list() as JSON
}
def show = {
return Brand.get(params.id) as JSON
}
def save = {
def brand = new Brand(name: params.name, description: params.description, logoImageURL: params.logoURL)
if (brand.save()) {
render brand as JSON
} else {
render brand.errors
}
}
}
============== Error message ===============
Request URL: http://localhost:8080/ShoesShop/brands
message /ShoesShop/WEB-INF/grails-app/views/brand/list.jsp
description The requested resource (/ShoesShop/WEB-INF/grails-app/views/brand/list.jsp) is not available.

It should work if you instead do:
def list = {
render Brand.list() as JSON
}

Related

REST API controller successfully creates a resource but returns 406 Not Acceptable

I have a RESTful API that when receiving a valid POST request, creates a resource on a repository (database) and returns the newly created object in JSON or XML with a HTTP 201 Created response header.
So I have configured my API to return HTTP 406 Not Acceptable when the consumer specifies an unknown response media type like so:
services.AddControllers(configure => configure.ReturnHttpNotAcceptable = true)
.AddNewtonsoftJson(setup =>
{
setup.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
})
.AddXmlDataContractSerializerFormatters()
This is my controller:
[ApiController]
[Route("api/v1/users")]
[Produces("application/json", "text/json", "application/xml", "text/xml")]
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
public class UsersController : ControllerBase
{
// [...]
[HttpGet("{userId}", Name = "GetUser")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public ActionResult<User> GetUser(int userId)
{
var user = _repository.GetUser(userId);
return user == null
? NotFound()
: Ok(_mapper.Map<User>(user));
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status303SeeOther)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
public ActionResult<User> CreateUser(UserForCreation userForCreation)
{
var existingUser = _repository.GetUser(userForCreation.Email);
if (existingUser != null)
{
return this.SeeOther("GetUser", new { userId = existingUser.Id });
}
var user = _mapper.Map<DataModel.User>(userForCreation);
_repository.AddUser(user);
_repository.Save();
var userToReturn = _mapper.Map<User>(user);
return CreatedAtRoute("GetUser", new
{
userId = userToReturn.Id
}, userToReturn);
}
}
So, let's say I send a request with an accept header like accept: text/plain; until here my resource is created on the repository, but then when I get to the CreatedAtRoute() call, ASP returns the so called HTTP 406 status code.
I understand why this is happening, what I want to know is how (if possible) to tell ASP to return HTTP 406 before calling CreateUser() of before creating the resource on the repository.

How can I do this redirection from ballerinalang?

I've implemented a REST endpoint in ballerinalang called https://localhost:9090/isValidUser. And here is my code below
import ballerina.net.http;
#http:configuration {
basePath:"/",
httpsPort:9090,
keyStoreFile:"${ballerina.home}/bre/security/wso2carbon.jks",
keyStorePassword:"wso2carbon",
certPassword:"wso2carbon",
trustStoreFile:"${ballerina.home}/bre/security/client-truststore.jks",
trustStorePassword:"wso2carbon"
}
service<http> authentication {
#http:resourceConfig {
methods:["POST"],
path:"/isValidUser"
}
resource isValidUser (http:Request req, http:Response res) {
println(req.getHeaders());
res.send();
}
}
Now I need to do is when I invoke that URL from the browser, I need to redirect the user to another URL called https://localhost:3000 after some validations happen within my service.
So how can I do this redirection from ballerinalang?
Ballerina has provided smooth API to do the redirection. Please check following code which elaborates the Listener endpoint redirection.
service<http:Service> redirect1 bind {port:9090} {
#http:ResourceConfig {
methods:["GET"],
path:"/"
}
redirect1 (endpoint client, http:Request req) {
http:Response res = new;
_ = client -> redirect(res, http:REDIRECT_TEMPORARY_REDIRECT_307,
["http://localhost:9093/redirect2"]);
}
}
The complete example is available in
Ballerina Redirects
In Ballerina, you need to handle the redirects yourself by setting the necessary headers and status code. The following example is a simple demo of how you can redirect in Ballerina. (note: I tried this in Ballerina 0.95.2)
import ballerina.net.http;
#http:configuration {basePath:"/hello"}
service<http> helloWorld {
#http:resourceConfig {
methods:["GET"],
path:"/"
}
resource sayHello (http:Request request, http:Response response) {
map qParams = request.getQueryParams();
var name, _ = (string)qParams.name;
if (isExistingUser(name)) {
response.setStringPayload("Hello Ballerina!");
} else {
response.setHeader("Location", "http://localhost:9090/hello/newUser");
response.setStatusCode(302);
}
_ = response.send();
}
#http:resourceConfig {path:"/newUser"}
resource newUser (http:Request request, http:Response response) {
string msg = "New to Ballerina? Welcome!";
response.setStringPayload(msg);
_ = response.send();
}
}
function isExistingUser (string name) (boolean) {
return name == "Ballerina";
}

Grails RestfulController doesn't respond with a JSON when Content-Type: application/x-www-form-urlencoded header is present

I'm trying to implement a simple RestfulController for my application.
Given the following domain class:
class Test {
String name
int someInteger
static constraints = {
}
}
and its controller:
class TestController extends RestfulController<Test>{
TestController() {
super(Test)
}
}
Inside conf/UrlMappings.groovy I added the following entries:
"/api/$controller?(.${format})?" {
action = [POST: "save", PUT: "save", GET: "index", DELETE:"error"]
}
"/api/$controller/$id?(.${format})?" {
action = [POST: "update", PUT: "update", GET: "show", DELETE: "delete"]
}
Get requests are working fine, but Post and Put requests to a URL like http://localhost:8080/app/api/test.json when the Content-Type: application/x-www-form-urlencoded Header is present fail to respond with a JSON as expected. Instead render the show action view after persisting the entrie sent.
I also tried to use the Header Accept: application/json with no effect.
How can I fix that?
Edit:
Further investigating RestfulController's source file and the docs section regarding Content Negotiation I was able fix it by overriding the save and update methods replacing the line:
request.withFormat {
with:
withFormat {
Is it intentional or is there a flaw on RestfulController's implementation?
Why does it consider the Content-Type header instead of the Accept header to render response?
If it's acceptable for all your controller's methods to always respond with JSON (when there a response body), you can achieve this with responseFormats like so:
class TestController extends RestfulController<Test>{
static responseFormats = ['json']
TestController() {
super(Test)
}
def customJsonAction() {
respond Something.get(params.id)
}
def someActionThatRendersGsp() {
render view: 'myGsp', model: [foo: 'bar']
}
}
This means the controller will always respond with JSON regardless of which headers, params, etc. are sent by the client.
Sorry for taking so long to respond. I had some trouble putting everything to work. Thanks a lot #Dónal for all the help. Ended using the following class to do the trick:
import org.codehaus.groovy.grails.web.servlet.HttpHeaders;
import org.springframework.http.HttpStatus;
import grails.artefact.Artefact;
import grails.rest.RestfulController;
import grails.transaction.Transactional;
#Artefact("Controller")
#Transactional(readOnly = true)
class MyRestfulController<T> extends RestfulController<T> {
public MyRestfulController(Class<T> resource, boolean readOnly = false) {
super(resource, readOnly);
}
#Override
#Transactional
def save() {
if(handleReadOnly()) {
return
}
T instance = createResource(getParametersToBind())
instance.validate()
if (instance.hasErrors()) {
respond instance.errors, view:'create' // STATUS CODE 422
return
}
instance.save flush:true
def formatHolder = params.format ? this : request
formatHolder.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: "${resourceName}.label".toString(), default: resourceClassName), instance.id])
redirect instance
}
'*' {
response.addHeader(HttpHeaders.LOCATION,
g.createLink(
resource: this.controllerName, action: 'show',id: instance.id, absolute: true,
namespace: hasProperty('namespace') ? this.namespace : null ))
respond instance, [status: HttpStatus.CREATED]
}
}
}
#Override
#Transactional
def update() {
if(handleReadOnly()) {
return
}
T instance = queryForResource(params.id)
if (instance == null) {
notFound()
return
}
instance.properties = getParametersToBind()
if (instance.hasErrors()) {
respond instance.errors, view:'edit' // STATUS CODE 422
return
}
instance.save flush:true
def formatHolder = params.format ? this : request
formatHolder.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: "${resourceClassName}.label".toString(), default: resourceClassName), instance.id])
redirect instance
}
'*'{
response.addHeader(HttpHeaders.LOCATION,
g.createLink(
resource: this.controllerName, action: 'show',id: instance.id, absolute: true,
namespace: hasProperty('namespace') ? this.namespace : null ))
respond instance, [status: HttpStatus.OK]
}
}
}
}
By using def formatHolder = params.format ? this : request and then call formatHolder.withFormat I am now able to override the response format independently from the request format.
It doesn't work for the Accept Header yet but at least it works.

How to exclude property of a collection in JSON rendering in Grails 2.3

I am trying to setup a rest webservice (JSON) this is what I am getting:
{"name":"test","routines":[{"class":"Routine","id":1},{"class":"Routine","id":2}]}
This is what I want to get:
{"name":"test","routines":[{"name": "routine-1"},{"name": "routine-2"}]}
I have these domains:
class Program {
String name;
static hasMany = [routines: Routine]
}
class Routine {
String name
}
I have this controller:
class ProgramController extends RestfulController {
static responseFormats = ['json']
def show(Program program) {
respond program
}
}
I added this in the resources.groovy
programRenderer(JsonRenderer, Program) {
excludes = ['class', 'id']
}
routineRenderer(JsonRenderer, Routine) {
excludes = ['class', 'id']
}
How do I include the name property of Routine in the json response using the show method/action of ProgramController?
The ObjectMarshaller approach is the technically correct way. However, the code is cumbersome to write and it's a maintenance headache syncing the fields of the domain with the marshaller.
In the spirit of being Groovy and keeping things really simple, we've been quite happy just adding a little out() method to each REST domain.
Program.groovy
class Program {
String name
static hasMany = [routines: Routine]
def out() {
return [
name: name,
count: routines?.size(),
routines: routines?.collect { [name: it.name] }
]
}
}
ProgramController.groovy
import grails.converters.JSON
class ProgramController {
def show() {
def resource = Program.read(params.id)
render resource.out() as JSON
}
}
JSON Response
{
name: "test",
count: 2,
routines: [{ name: "routine-1" }, { name: "routine-2" }]
}
The out() method approach makes it easy to customize the response JSON, such as adding count for the number of routines.

Grais Rest: get a Json object from a "lastName" and not "id"

How get a user of this class, from a lastName, not from the id.
in my example i use a REST web service.
My class USER.groovy in Grails:
class User {
String firstName
String lastName
String zipcode }
class UrlMappings.groovy
class UrlMappings {
static mappings = {
/user/$id" (controller: "user") {
action = [GET: "show"]
}
}
}
def show in UserController.groovy
def show = {
User user = User.get(params.id)
if (user) {
render user as JSON
} else {
SendNotFoundResponse()
}
}
As I understand, your problem that you don't know how to query domain by other fields that id. For current example you can use:
User.findByFirstName(params.id)
And, please read about GORM querying - http://grails.org/doc/latest/guide/GORM.html#querying