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.

Deserialization/Serialization issue when json is injected via spring annotations

See original GitHub issue
What steps will reproduce the problem?

1. Create the following java object

public class Search {

    private String accountNumber = "";
    public String getAccountNumber() {
        return accountNumber;
    }
    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }
    public String getConfirmationNumber() {
        return confirmationNumber;
    }
    public void setConfirmationNumber(String confirmationNumber) {
        this.confirmationNumber = confirmationNumber;
    }
    public String getBankNumber() {
        return bankNumber;
    }
    public void setBankNumber(String bankNumber) {
        this.bankNumber = bankNumber;
    }
    private String confirmationNumber = "";
    private String bankNumber = "";


}


2. Create a controller class in Spring and add this method



@Controller("transactionController")
@RequestMapping("/transaction/*.jspx")
public class TransactionController {

     @RequestMapping(value = "lookup.jspx",
              method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView lookup(@RequestParam("json") String json) throws 
Exception {

        String jsonString = "";


        try {


     Gson gson = new Gson();
     System.out.println(json); //JSON prints fine here
         Search ns = gson.fromJson(jsonString, Search.class); //runtime 
Error occurs here, everytime

   Replace the above code to this and it works fine.

            JsonElement element = new JsonParser().parse(json);
            gson.fromJson(element , Search.class);


}

3. Create a Junit test method inside another class. Run this test method.


    @SpringBeanByName
    public TransactionController transactionController;


    @Test
    public void testThis() throws Exception{


        Gson gson = new Gson();
        Search search = new Search();
        search.setAccountNumber("5794749");
        String json = gson.toJson(search);
        transactionController.lookup(json);
     }





4. You get this error 

{"accountNumber":"5794749","confirmationNumber":"","bankNumber":""}
05:56:00,288 ERROR 
[com.bbvacompass.webpromises.web.spring.mvc.TransactionController]  Failed 
to generate JSON!
com.google.gson.JsonParseException: Failed parsing JSON source: 
java.io.StringReader@51165116 to Json
    at com.google.gson.JsonParser.parse(JsonParser.java:57)
    at com.google.gson.Gson.fromJson(Gson.java:376)
    at com.google.gson.Gson.fromJson(Gson.java:329)
    at com.google.gson.Gson.fromJson(Gson.java:305)
    at 
com.bbvacompass.webpromises.web.spring.mvc.TransactionController.lookup
(TransactionController.java:100)


5. If you write a simple test, it works fine. 



    @Test
    public void testJSONSearch(){

        Search search = new Search();
        search.setAccountNumber("123456");
        Gson gson = new Gson();
        String json = gson.toJson(search);
        search = gson.fromJson(json, Search.class);

    }



What is the expected output? What do you see instead?

You should get the object back either way


What version of the product are you using? On what operating system?


Gson 1.3 on an IBM 1.6 JVM, but I believe that if you follow the exact 
steps above and inject the value of json, it will bomb using the fromJson
(String, Object) method. The fromJson(Jsonelement, class) works fine.



Please provide any additional information below.

Original issue reported on code.google.com by chrislha...@gmail.com on 7 Aug 2009 at 11:07

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
iBearcatcommented, Jul 20, 2018

nice

0reactions
GoogleCodeExportercommented, Mar 19, 2015

Original comment by inder123 on 10 Oct 2009 at 6:06

  • Changed state: Invalid
Read more comments on GitHub >

github_iconTop Results From Across the Web

Serialization and Deserialization Issues in Spring REST
Spring Boot projects primarily use the JSON library Jackson to serialize and deserialize objects. It is especially useful that Jackson ...
Read more >
Jackson Annotations for JSON - Spring Framework Guru
annotation tells Jackson to deserialize the JSON into Java object using the name given in the setter method. Use this annotation when your...
Read more >
Using @JsonComponent in Spring Boot - Baeldung
The annotation allows us to expose an annotated class to be a Jackson serializer and/or deserializer without the need to add it to...
Read more >
java - De-serializing JSON to polymorphic object model using ...
Try to register subtype using ObjectMapper.registerSubtypes instead of using annotations.
Read more >
Spring HATEOAS - Reference Documentation
Following the approach from above would cause two problems: ... EntityLinks is available via dependency injection by activating ...
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