I want to convert JSON to java
my json
var eateryRatingFactory = function() {
var self = this;
self.dineInMenu = [{
header: "Food",
items: [{
name: "Quality",
id: '1',
rating: '0'
},.....so on ]
},......so on ];
self.deliveryMenu = //some values same as beloew
};
my java class
public class EateryRatingAdapter {
public List<Ratings> dineInMenu;
public List<Ratings> deliveryMenu;
//setters and getters
public class Ratings{
public String header;
public List<Item> items;
//setters and getters
public class Item{
public String name;
public String id;
public String rating;
//setters and getters
}
}
}
while i'm getting the JSON at Request body , it is giving 400 bad request mean syntax mistake can u guys help this out
Use Object Mapper
ObjectMapper mapper = new ObjectMapper();
// Your JSON should cater your class
String jsonString = "{"header": "Food" , ................}";
//JSON from String to Object
**Ratings obj = mapper.readValue(jsonString, Ratings.class)**
Related
Im trying to parse trough json string and return values into custom meta data type field on Case. I have created the custom metadata type already, example:
<label>Jordfeil.Casefield</label>
<protected>false</protected>
<values>
<field>CaseField__c</field>
<value xsi:type="xsd:string">Casefield</value>
</values>
<values>
<field>Context__c</field>
<value xsi:type="xsd:string">JORDFEIL</value>
</values>
<values>
<field>JsonField__c</field>
<value xsi:type="xsd:string">key1</value>
</values>
</CustomMetadata>
Json String :
{
"operation": "get-id",
"payload": {
"ApplicationContext": {
"Context": "JORDFEIL",
"SenderId": "xxx",
"subContext": ""
},
"supportIssue": {
"InfoFields": [
{
"Key": "key1",
"Value": "value1"
},
{
"Key": "key2",
"Value": "value2"
},
{
"Key": "key3",
"Value": "value3"
}
],
}
},
"type": "~:CustomerInquiry",
}
My Wrapper class:
public class WebskjemaModel {
public class ApplicationContext {
public String Context;
public String SenderId;
public String subContext;
}
public String operation;
public Payload payload;
public String type;
public class InfoFields {
public String Key;
public String Value;
}
public class Payload {
public ApplicationContext ApplicationContext;
public SupportIssue supportIssue;
}
public class SupportIssue {
public List<InfoFields> InfoFields;
public String assignId;
public String businessObjectType;
public String comment;
public String contactDate;
public String contactName;
public String customerName;
public String customerNo;
public String docClass;
public String format;
public String objectDescription;
public String objectId;
public String subject;
}
public static WebskjemaModel parse(String json) {
return (WebskjemaModel) System.JSON.deserialize(json, WebskjemaModel.class);
}
}
The json string is stored a field in Case called WebskjemaBlob__c.
Below is my class:
public with sharing class WebskjemaCaseCreator {
Map<String, Case> Webskjemastring = new Map<String, Case>();
private Map<Id, Case> cases { get; set; }
private Map<Id, WebskjemaModel> webskjemaModels = new Map<Id, WebskjemaModel>();
public WebskjemaCaseCreator(Map<Id, Case> cases) {
this.cases = cases;
deserializeJson();
mapFields();
}
private void deserializeJson() {
for (Id caseId : cases.keySet()) {
Case c = cases.get(caseId);
WebskjemaModel model = WebskjemaModel.parse(c.Webskjemablob__c);
webskjemaModels.put(caseId, model);
}
}
private void mapFields() {
for (Id caseId : cases.keySet()) {
Case c = cases.get(caseId);
WebskjemaModel model = webskjemaModels.get(caseId);
}
}
private void mapFieldsForSingleCase(Case c, WebskjemaModel model) {
// TODO: Find WebskjemaModel inforfields and loop
// for (list of infofields for this model only){
// TODO: call method mapInfoField
// }
}
private void mapInfoField(Case c, String context, String infoFieldKey, String infoFieldValue) {
// TODO: Find Case field from custom meta data type mapping, based on context and infoFielfKey
// TODO: Put value from inforFieldValue into Case field
}
}
My question is how you get the key value pair from infofields and update the case fields based on the custom meta data type mapping?
import javax.ws.rs.FormParam;
import com.alibaba.fastjson.JSON;
Hi guys, The JAX-RS seems cannot init the list field in resteasy 2.x/3.x/4.x automatically,
Here is my VO used in servce
public class OrderVO {
#FormParam("id")
private long id;
#FormParam("title")
private String title;
#FormParam("product")
private List<Integer> product;
...
public String toString() {
return JSON.toJSONString(this);
}
}
And here is my service method:
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED+";charset=UTF-8")
#Path("/create")
public InsertResult saveOrder(#BeanParam OrderVO order) {
logger.debug(order.toString());
}
Other parameters are auto inited except the List or Integer[], and can anyone tell me why....
I have problem while casting an unknown enum to a default enum in spring boot while using mongo repository.
This is the enum.
public enum EventType implements Serializable
{
WORKDONE("WORKDONE"),
ODRCOM("ODRCOM"),
EXECUTED("EXECUTED"),
REBOOK("REBOOK"),
MANUAL("MANUAL"),
UNKNOWN("UNKNOWN");
private String value;
EventType(final String type) {
this.value = type;
}
#Override
public String toString() {
return value;
}
}
And here is my model class
#JsonInclude(JsonInclude.Include.NON_NULL)
public class Event {
//other properties
#JsonProperty("eventType")
private EventType eventType;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
#JsonProperty("eventType")
public EventType getEventType() {
return eventType;
}
#JsonProperty("eventType")
public void setEventType(String eventType) {
this.eventType = Optional.ofNullable(EventType.valueOf(eventType)).orElse(EventType.UNKNOWN);
}
//other getters and setters
}
Here is the mongo repository
public interface EventRepository extends MongoRepository<Event, String> {
}
The document stored in the db is of the following stucture
{
...
"eventType" : "REBOOK1",
...
}
Please note that the REBOOK1 is not a valid enum. But the setter should be able to able to cast anything else to type UNKNOWN.
However it gives this exception everytime
No enum constant dk.nuuday.ossieventprocessor.app.model.EventType.REBOOK1
I have tried with adding a custom converter as a configuration but no luck
Any help is greatly appreciated
How can we convert the Flux< Employe> to Mono< Customers > object?
Flux< Employe> empFlux = getService(); // It will return list of Employe Employe { private String id; private String info;}
// need transform the empFlux data to Mono< Customers>
public class CusData {
private String id;
private String dept;
private String info;
public String getId() {
return id;
}
}
public class Customers {
private List<CusData> cusDataList;
public List<CusData> getCusDataList() {
return cusDataList;
}
public void setCusDataList(List<CusData> cusDataList) {
this.cusDataList = cusDataList;
}
}
public class Employe {
private String id;
private String info;
}
If I understood your code, you must have something like that:
Mono<Customers> customers = getService().map( employee -> CusData.builder()
.id( employee.getId() )
.info( employee.getInfo() )
.build() )
.collectList()
.map( cusDatas -> Customers.builder()
.cusDataList( cusDatas )
.build() );
Flux has a handy method collectList() which takes care of performing the transformation for you.
I have used the String in the example below.
Code Snippet below.
Flux<String> stringFlux = Flux.just("Spring", "Spring Boot", "Reactive Spring")
.log();
Mono<List<String>> stringMono = stringFlux.collectList();
my URI is
http://localhost:8080/context/my-objects/search/findByCode?code=foo
JSON response:
{
"_embedded" : {
"my-objects" : [ {
"code" : "foo",
"description" : "foo description",
"_links" : {
"self" : {
"href" : "http://localhost:8080/context/my-objects/34"
}
}
} ]
}
}
How can I get a java MyObject with Traverson or RestTemplate?
import org.springframework.hateoas.ResourceSupport;
public class MyObject extends ResourceSupport{
private String code;
private String description;
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public String getCode() {
return code;
}
public void setCode(final String code) {
this.code = code;
}
}
This is my Template. I've also tried with a default one.
{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper);
RestTemplate template = new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
}
Thx in advance.
I've found the solution. First, create a Resources class:
import org.springframework.hateoas.Resources;
public class MyObjects extends Resources<MyObject> { }
Then it is straightforward:
MyObjects myObjects = template.getForObject("http://localhost:8080/context/my-objects/search/findByCode?code=foo", MyObjects.class);
Attention: the template should support hal+json Media Type.
Or with Traverson:
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.client.Traverson;
Traverson traverson;
try{
traverson = new Traverson(new URI("http://localhost:8080/context"), MediaTypes.HAL_JSON);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("code", "foo");
MyObjects myObjects = traverson.follow("my-objects", "search", "findByCode").withTemplateParameters(
parameters).toObject(MyObjects.class);
} catch (URISyntaxException e) {}
If you don't want to extend your POJO MyObject with ResourceSupport class, your Resources class should be typed with Resource:
import org.springframework.hateoas.Resource;
import org.springframework.hateoas.Resources;
public class MyObjects extends Resources<Resource<MyObject>> { }
(If you don't need links the type parameter may again be MyObject).