I have a integer field in a class which I have made private and no setter method is provided but when I generate the client with WSDL2Java, the generated class has a setter method.
Why is it so?
I am using Apache Axis 2.
I am putting the code for more explanation
public class MyClass {
private int id;
public int getId() {
return id;
}
}
So what should I do to hide the setter method when client is generated.
When you creating a web service using a POJO it generates a wsdl file.
What WSDL2java tool uses is that generated wsdl file. But that does not have any idea of the original POJO you use to generate the wsdl. Currently there is no way in Axis2 to generate only the getter methods of the bean classes.
Related
I have a abstract class and two implementations:
public abstract class Attribute {
// some properties
}
public class CustomAttribute extends Attribute{
private String property1;
}
public class DefaultAttribute extends Attribute{
private String property2;
}
There's another class, which includes these attributes:
public class Step{
private List<Attribute> attributes;
}
Now when Step gets serialized, the self link is missing. I need the self reference, since I want to update the attributes. According to the documentation, jackson needs a little help deciding which class to use. But that does not help, because I need to use both classes. So I build a custom serializer (and registered with a module) for Step and now I wonder how I can construct the link myself. I couldn't find anything in the Spring Data Rest docs regarding this. Since Spring Data Rest adds these links automatically, I think there might be a way to have the protocol/hostname/port information available in the JsonSerializer. How do I get the information in my custom serializer?
Ok, now I use the linkTo() function to get the hostname and port and I manually set the rest of the resource URL in my custom serializer.
final Link attributeLink = linkTo(CustomAttributeRepository.class)
.slash("/api")
.slash("customAttributes")
.slash(attribute.getIdentifier()).withSelfRel();
//#formatter:off
jsonGenerator.writeFieldName("_links");
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("self");
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("href", attributeLink.getHref());
jsonGenerator.writeEndObject();
jsonGenerator.writeEndObject();
//#formatter:on
With following JAX-RS resource class,
#Path("/myresource")
class MyResource {
#GET
public Response readSomeValue() {
// ...
}
#Inject
private int someValue;
}
How can I inject someValue?
I'm using org.glassfish.jersey.bundles:jaxrs-ri with Spring on Apache Tomcat. No EJBs.
I, so far, found this.
/**
* Is this gonna work?
*/
class SomeValueProducer {
#Produces
public int produceSomeValue() {
/// ...
}
}
Is this the only way? Using #Procudes?
Where can I place the producer class? Just alongside the resource class?
Do I need a beans.xml?
Do I need a qualifier annotation?
Thanks.
Is this the only way? Using #Procudes?
Yes
Where can I place the producer class? Just alongside the resource class?
doesnt matter, every jar with a valid beans.xml should be scanned from the framework if configured correct: including every package in your project.
Do I need a beans.xml?
yes
Do I need a qualifier annotation?
yes, without a qualififer every method which returns an int value is a possible source for an injection.
One of my WCF functions returns an object that has a member variable of a type from another library that is beyond my control. I cannot decorate that library's classes. In fact, I cannot even use DataContractSurrogate because the library's classes have private member variables that are essential to operation (i.e. if I return the object without those private member variables, the public properties throw exceptions).
If I say that interoperability for this particular method is not needed (at least until the owners of this library can revise to make their objects serializable), is it possible for me to use WCF to return this object such that it can at least be consumed by a .NET client?
How do I go about doing that?
Update: I am adding pseudo code below...
// My code, I have control
[DataContract]
public class MyObject
{
private TheirObject theirObject;
[DataMember]
public int SomeNumber
{
get { return theirObject.SomeNumber; } // public property exposed
private set { }
}
}
// Their code, I have no control
public class TheirObject
{
private TheirOtherObject theirOtherObject;
public int SomeNumber
{
get { return theirOtherObject.SomeOtherProperty; }
set { // ... }
}
}
I've tried adding DataMember to my instance of their object, making it public, using a DataContractSurrogate, and even manually streaming the object. In all cases, I get some error that eventually leads back to their object not being explicitly serializable.
Sure, write a wrapper class that has all of the same public properties available and simply put "get { return internalObject.ThisProperty; }. Decorate the wrapper class so that it works with WCF.
Another option is to write a Proxy class which mirrors the properties of the type you wish to use exactly, and return that via WCF.
You can use AutoMapper to populate the proxy object.
This approach has the advantage that your service's consumers don't need to take a dependency on the third party library in trying to use it.
I have a WCF service and I'm sharing types with a client in a shared assembly.
If the client create a derived class will it be possible to pass back the derived type to the service so that I can read the added properties through reflection ?
I tried but having issues with KnownTypes since the service don't know how to deserialize the derived type.
[Serializable]
public abstract class Car : ICar
{........
//on the client :
[Serializable]
public class MyCar : Car
{......
when passing myCar to Service I get the exception complaining about knownType but I cant add this on the server since I wont know what the client will be sending through and I want to handle extra properties through reflection.
Possible to register client types as knowntypes at runtime ?
Is this maybe the solution ?
http://blogs.msdn.com/b/sowmy/archive/2006/03/26/561188.aspx
This is not possible. Both service and client has to know what types will be sent in messages. If you want to use known type you have to define that relation to parent type on the service.
Why do you need to know added properties on the server?
I think there is a way.
I vaguely remember that when I studied WCF, I met ExtensionData which should be a mechanism to get everything that does not match the serialization of the class. for example, if you enable ExtensionData and you are in this situation
//Server
public class GenericRQ
{
public string GenericProperty {get;set;}
}
public Service GenericService
{
Public void GenericMethod(GenericRQ RQ)
{
}
}
// client
Public class MoreSpecificRQ : GenericRQ
{
public string SpecificProperty {get;set;}
}
At
Public void GenericMethod(GenericRQ RQ)
{
// the serializer adds automatically in RQ.ExtensionData everything that has come and that does not match the class GenericRQ.
}
On how to enable ExtensionData you to easily search on the web
i got the problem, that a WCF service ( generated with svcutil.exe ) generates it's own datatypes, instead of using the ones i already defined..
for example:
The svcutil generated something like this:
public partial class EmailTransactionRequestMsg : object, System.Runtime.Serialization.IExtensibleDataObject
{
private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
private int bit_to_setField;
private string country_db_identifierField;
.
.
}
when i actually want it to use the class already exists:
[DataContract(Namespace = "Ps.App.Mailing.MsgQueue.MsgInterfaces")]
public class EmailTransactionRequestMsg
{
[DataMember]
public string country_db_identifier;
[DataMember]
public int bit_to_set;
}
i see that the svcutil service creates a new extensionData-field ( which i don't know for which purpose this is required )
So, how do i get svcutil to use my own class ( because i don't want to cast the objects by every single field )
Thank you all!
Please take a lookat the following svcutil reference:
http://msdn.microsoft.com/en-us/library/aa347733.aspx
especially the /reference: switch which should be exactly what you're looking for.
i see that the svcutil service creates
a new extensionData-field ( which i
don't know for which purpose this is
required )
This is generated for you to help with data contract versioning. If you add a new property to EmailTransactionRequestMsg later but have a client that is using an older assembly without that property defined it will still be able to use the new service and ExtensionDataObject will contain unknown (new) property(ies). Again MSDN should sprovide you with more details: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.extensiondataobject.aspx