question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement Content Negotiation feature

See original GitHub issue

Currently, if I want to send back a response in JSon, I have to do

install(GsonSupport)

This feels like it’s coupling a specific implementation of Json encoding with the feature of actually representing a response with a specific content type.

Considering generalising this support with something more generic such as

install(AutoContentType)

and register different content types with specific serialisers/deserialisers. For instance

register("application/json", GsonSerialiser)
register("application/xml", XmlSerialiser)

and then allow the respond call to specify the content type

call.respond(listOf(myStuff), "application/json")

which will be picked up by the correct serialiser.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
orangycommented, Aug 10, 2017

@McPringle agree, that’s the plan. It should be something like this:

install(AutoContent) {
   register(ContentType.Application.Json, GsonConverter)
}

routing { 
   get("/api/user/{id}") {
      val id = call.parameters["id"]
      val user = db.getUser(id)
      call.respond(user)
   }
}

(error handling omitted for clarity)

Same for call.receive<User>() for POST data.

0reactions
cy6erGn0mcommented, Aug 30, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Content negotiation - HTTP - MDN Web Docs
In HTTP, content negotiation is the mechanism that is used for serving different representations of a resource to the same URI to help...
Read more >
Content Negotiation using Spring MVC
In this post I want to discuss how to configure and use content negotiation with Spring, mostly in terms of RESTful Controllers using...
Read more >
Content Negotiation Implementing Support for XML - Javatpoint
Content negotiation is the process of selecting the best representation for a given response when there are multiple representations available. It is a...
Read more >
Spring MVC Content Negotiation - Baeldung
A guide to configuring content negotiation in a Spring MVC application and on enabling and disabling the various available strategies.
Read more >
Implement Content Negotiation using Spring Boot - JavaInUse
In this post we implement a simple Spring Boot example to implement Content Negotiation for returning XML or JSON depending upon the URL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found