class Solution{
static void main(String[] args){
("write a code here")
}
}
// object class
class Car{
public String Brand = "Honda";
public int year = 2009;
public String Model = "City AMT";
}
Related
Here's very simple scenario in which I got value object that I want to un-wrap for serialization. Using custom Serializer is not an option.
public class UnwrappedWithPropertyName {
public static void main(String[] args) throws JsonProcessingException {
final Address address = new Address(new Postcode("45678"));
final ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(address));
}
static class Address {
#JsonUnwrapped
#JsonProperty("postcode")
private final Postcode postcode;
Address(Postcode postcode) {
this.postcode = postcode;
}
public Postcode getPostcode() {
return postcode;
}
}
static class Postcode {
private final String value;
Postcode(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
That will result in {"value":"45678"} and what I would expect is {"postcode":"45678"}
By annotating field with #JsonValue one can control the name of such field from enclosing class.
static class Address {
#JsonProperty("postcode")
private final Postcode postcode;
Address(Postcode postcode) {
this.postcode = postcode;
}
public Postcode getPostcode() {
return postcode;
}
}
static class Postcode {
#JsonValue
private final String value;
Postcode(String value) {
this.value = value;
}
}
Move #JsonProperty("postcode") to private final String value;
I have a simple Product class as it follows
#SolrDocument(collection = "product")
public class Product {
#Id
#Indexed(name = "id", type = "string")
private String id;
#Field
#Indexed(name = "namex", type = "text_general", stored = false, searchable=true)
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
my problem is that the annotation #Indexed is completely ignored. The name of the field is simply name (instead of namex) and the field is stored. Any guess?
UPDATE 1 if I remove the type annotation name works, but stored has no effect still
I managed by modifying the bean that creates the SolrTemplate object like follows:
#Bean
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
SolrTemplate st = new SolrTemplate(client);
st.setSchemaCreationFeatures(Collections.singletonList(Feature.CREATE_MISSING_FIELDS));
st.afterPropertiesSet();
return st;
}
I have written a wcf program where I have an class library where I declared the function which contains a class as a parameter.
I accessed that function in a client program but I don't know how to pass the class to that function. I will write the code below.
I have an interface which contains basic function declaration
In the class library there is another class it is implementing the interface. That interface contains a method which takes a class as parameter.
That class which is parameter contains properties.
[ServiceContract]
public interface ICarDetails
{
[OperationContract]
string updateCarDetails(Car c);
}
public class CarDetails : ICarDetails
{
public string updateCarDetails(Car c)
{
//some operations and initilizations
string example = Car.carno = "1234";
return "success";
}
}
Public class Car
{
private string carno;
private string carModel;
public string CARNO
{
get{ return carno; }
set{ carno = value; }
}
public string CARMODEL
{
get{ return carModel; }
set{ carModel = value; }
}
}
3) I will get access this function in myclient program where I consume. While consuming I need to send a class right? If so how can I send a class.
class Program
{
static void Main(string[] args)
{
CarDetailsserviceClient client = new CarDetailsserviceClient();
string abc = client.updateCarDetails(); // This shows error
}
}
public class carclient
{
public string carno = "6789";
}
I want to send this client class carclient to wcf service function updatecardetails.
You need to mark your Car type with the DataContract and DataMember attributes before you can pass it across the service boundary:
[DataContract]
public class Car
{
private string carno;
private string carModel;
[DataMember]
public string CARNO
{
get{ return carno; }
set{ carno = value; }
}
[DataMember]
public string CARMODEL
{
get{ return carModel; }
set{ carModel = value; }
}
}
If you do this then regenerate your service client, you will find you have access to the Car type from you calling code and you won't need to define your own type on the client side.
You can't pass a class, you must pass an object of that class. Create new object :
Car carclient = new Car() { CARNO = "6789" };
Then, pass it in argument :
string abc = client.updateCarDetails(carclient);
Car must be include in the DataContract like this :
[DataContract]
public class Car
{
private String carno;
private String carModel;
[DataMember]
public String CARNO
{
get { return this.carno; }
set { this.carno = value; }
}
[DataMember]
public String CARMODEL
{
get { return this.carModel; }
set { this.carModel = value; }
}
}
I have spent hours looking how to solve this. When I try to get parent from a child all but it's id fields are empty. It just makes no sense. I am using PlayFramework 2.0.4 if that might indicate anything (besides a terrible choice of framework).
TRoute.java (parent)
#Entity
#Table(name="routes")
public class TRoute extends Model {
#Id
public String route_id;
public String agency_id;
#Constraints.Required
public String route_short_name;
#Constraints.Required
public String route_long_name;
public String route_desc;
#Constraints.Required
public String route_type;
public String route_url;
public String route_color;
public String route_text_color;
#OneToMany(mappedBy="troute")
public List<Trip> trips;
public static Finder<String, TRoute> find = new Finder(
String.class, TRoute.class
);
}
Trip.java (child)
#Entity
#Table(name="trips")
public class Trip extends Model {
#Constraints.Required
public String route_id;
#Constraints.Required
public String service_id;
#Id
public String trip_id;
public String trip_headsign;
public String direction_id;
public String block_id;
public String shape_id;
#ManyToOne
#JoinColumn(name="route_id")
public TRoute troute;
public static List<Trip> byRouteId(String route_id) {
List<Trip> trips =
Trip.find
.where().like("route_id", route_id)
.findList();
return trips;
}
public static Finder<String, Trip> find = new Finder(
String.class, Trip.class
);
}
The finder has a fetch() method which can be used to load properties of the other table. Something like:
public static List<Trip> byRouteId(String route_id) {
List<Trip> trips = List<Trip> trips = Trip.find
.fetch("troute") // fetch the other table's properties here
.where()
.like("route_id", route_id)
.findList();
return trips;
}
I have the following objects:
#JsonFilter("myFilter")
public class Person {
private Name name;
private int age;
public Name getName() {return name;}
public void setName(Name name) {this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
}
#JsonFilter("myFilter")
public class Name {
private String firstName;
private String lastName;
public String getFirstName() {return firstName;}
public void setFirstName(String firstName) {this.firstName = firstName;}
public String getLastName() {return lastName;}
public void setLastName(String lastName) {this.lastName = lastName;}
}
I wrote a method to marshall a Person object like this:
#Test
public void test() throws Exception {
Person person = new Person();
person.setAge(10);
Name name = new Name();
name.setFirstName("fname");
name.setLastName("lastname");
person.setName(name);
ObjectMapper mapper = new ObjectMapper();
FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter",
SimpleBeanPropertyFilter.filterOutAllExcept("name.firstName"));
System.out.println(mapper.filteredWriter(filters).writeValueAsString(person));
}
What I'd like to see is JSON like this:
{"name":{"firstName":"fname"}}
Is something like that possible?
Ok, figured it out. Varargs would have made this a bit prettier, but oh well. Just hope I don't have two inner beans which have properties with the same name. I wouldn't be able to make the distinction between the two
FilterProvider filters = new SimpleFilterProvider()
.addFilter("myFilter", SimpleBeanPropertyFilter
.filterOutAllExcept(new HashSet<String>(Arrays
.asList(new String[] { "name", "firstName" }))));
There's a better way that solves problem with property name conflicts. Just add another filter to class Name ("nameFilter"):
#JsonFilter("personFilter")
public class Person {
private Name name;
private int age;
public Name getName() {return name;}
public void setName(Name name) {this.name = name;}
public int getAge() {return age;}
public void setAge(int age) {this.age = age;}
}
#JsonFilter("nameFilter")
public class Name {
private String firstName;
private String lastName;
public String getFirstName() {return firstName;}
public void setFirstName(String firstName) {this.firstName = firstName;}
public String getLastName() {return lastName;}
public void setLastName(String lastName) {this.lastName = lastName;}
}
And then add 2 filters, one for Person and one for Name:
FilterProvider filterProvider = new SimpleFilterProvider()
.addFilter("personFilter", SimpleBeanPropertyFilter.filterOutAllExcept("name"))
.addFilter("nameFilter", SimpleBeanPropertyFilter.filterOutAllExcept("firstName"));