There is no constructor for line constructors / factory to deserialize from String ('') value

I ran into a json parsing problem when using the ObjectMapper class from the com.fasterxml.jackson.databind package, and the error I get is this:

 com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.graybar.utilities.ups.beans.Address: no String-argument constructor/factory method to deserialize from String value ('') 

The web application where this problem occurs is a Spring MVC application using the AngularJS front-end, but I can duplicate the problem with a much smaller, all java program. Here is my beans:

Shipment.java

 @JsonIgnoreProperties(ignoreUnknown = true) public class Shipment { @JsonProperty("Activity") private ArrayList<Activity> activity; public ArrayList<Activity> getActivity() { return activity; } public void setActivity(ArrayList<Activity> activity) { this.activity = activity; } } 

Activity.java

 @JsonIgnoreProperties(ignoreUnknown = true) public class Activity { @JsonProperty("ActivityLocation") private ArrayList<ActivityLocation> activityLocation; public ArrayList<ActivityLocation> getActivityLocation() { return activityLocation; } public void setActivityLocation(ArrayList<ActivityLocation> activityLocation) { this.activityLocation = activityLocation; } } 

ActivityLocation.java

 @JsonIgnoreProperties(ignoreUnknown = true) public class ActivityLocation { @JsonProperty("Address") private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } } 

Address.java

 @JsonIgnoreProperties(ignoreUnknown = true) public class Address { @JsonProperty("City") private String city; @JsonProperty("StateProvinceCode") private String stateProvinceCode; @JsonProperty("CountryCode") private String countryCode; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getStateProvinceCode() { return stateProvinceCode; } public void setStateProvinceCode(String stateProvinceCode) { this.stateProvinceCode = stateProvinceCode; } } 

Here is the code where I can correctly match json:

 public static void main(String[] args) { String jsonMessage = "" + "{" + " \"Activity\": [{ " + " \"ActivityLocation\": { " + " \"Address\": { " + " \"City\": \"Hana\", " + " \"StateProvinceCode\": \"Hi\", " + " \"CountryCode\": \"US\" " + " } " + " } " + " }, " + " { " + " \"ActivityLocation\": { " + " \"Address\": { " + " \"City\": \"Honolulu\", " + " \"StateProvinceCode\": \"Hi\", " + " \"CountryCode\": \"US\" " + " } " + " } " + " }] " + "} "; try { ObjectMapper mapper = new ObjectMapper(); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); Shipment shipment = mapper.readValue(jsonMessage, Shipment.class); System.out.println("shipment.toString = " + shipment.toString()); } catch (Exception e) { e.printStackTrace(); } } 

When setting data in jsonMessage var, when I run the error that I mentioned above:

  "{" + " \"Activity\": [{ " + " \"ActivityLocation\": { " + " \"Address\": { " + " \"City\": \"Hana\", " + " \"StateProvinceCode\": \"Hi\", " + " \"CountryCode\": \"US\" " + " } " + " } " + " }, " + " { " + " \"ActivityLocation\": { " + " \"Address\": \"\" " + " } " + " } " + " }] " + "} "; 

So, the problem occurs when changing json from this:

 { "ActivityLocation": { "Address": { "City": "Honolulu", "StateProvinceCode": "Hi", "CountryCode": "US" } } }] 

:

 { "ActivityLocation": { "Address": "" } } 

Instead of sending values ​​for my Address bean, I get only an empty string. Unfortunately, I receive data from a third party and cannot control the data that I receive.

Is there an annotation that needs to be added to be able to handle this?

+29
source share
3 answers

Try setting mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true)

or

 mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); 

depending on your version of Jackson.

+27
source

It was when I accidentally called

 mapper.convertValue(...) 

instead

 mapper.readValue(...) 

So, just make sure you call the correct method, as the argument is the same and the IDE can find many things

0
source
 mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); 

My code works the same as the answer above. The reason is that JSON from Jackson is different from JSON sent from the controller.

 String test1= mapper.writeValueAsString(result1); 

And json is similar (which can be deserialized normally):

 {"code":200,"message":"god","data":[{"nics":null,"status":null,"desktopOperatorType":null,"marker":null,"user_name":null,"user_group":null,"user_email":null,"product_id":null,"image_id":null,"computer_name":"AAAA","desktop_id":null,"created":null,"ip_address":null,"security_groups":null,"root_volume":null,"data_volumes":null,"availability_zone":null,"ou_name":null,"login_status":null,"desktop_ip":null,"ad_id":null},{"nics":null,"status":null,"desktopOperatorType":null,"marker":null,"user_name":null,"user_group":null,"user_email":null,"product_id":null,"image_id":null,"computer_name":"BBBB","desktop_id":null,"created":null,"ip_address":null,"security_groups":null,"root_volume":null,"data_volumes":null,"availability_zone":null,"ou_name":null,"login_status":null,"desktop_ip":null,"ad_id":null}]} 

but send JSON from another service just like:

 {"code":200,"message":"查询桌面列表成功","data":[{"nics":"","status":"","metadata":"","desktopOperatorType":"","marker":"","user_name":"csrgzbsjy","user_group":"ADMINISTRATORS","user_email":"","product_id":"","image_id":"","computer_name":"B-jiegou-all-15","desktop_id":"6360ee29-eb82-416b-aab8-18ded887e8ff","created":"2018-11-12T07:45:15.000Z","ip_address":"192.168.2.215","security_groups":"","root_volume":"","data_volumes":"","availability_zone":"","ou_name":"","login_status":"","desktop_ip":"","ad_id":""},{"nics":"","status":"","metadata":"","desktopOperatorType":"","marker":"","user_name":"glory_2147","user_group":"ADMINISTRATORS","user_email":"","product_id":"","image_id":"","computer_name":"H-pkpm-all-357","desktop_id":"709164e4-d3e6-495d-9c1e-a7b82e30bc83","created":"2018-11-09T09:54:09.000Z","ip_address":"192.168.2.235","security_groups":"","root_volume":"","data_volumes":"","availability_zone":"","ou_name":"","login_status":"","desktop_ip":"","ad_id":""}]} 

You may notice a difference when working with a parameter without initiation. Be careful

-3
source

Source: https://habr.com/ru/post/1269844/


All Articles