I try to write an application to create PDF files, it uses the JavaServer Faces. I struggle with a problem when I give the value of the text box from bean to factory class, the values are lost. I wonder why this happens, already tried many solutions, but they will cause expansion of the code and do not help.
Bean code:
#ManagedBean
#ViewScoped
public class PanelInneBean extends AbstractPanel implements Serializable {
private static final long serialVersionUID = 1L;
private final int CODE = 4;
private boolean use;
private String tytul, opis;
private PdfFactory pdf = PdfFactory.getPdfObject();
public PanelInneBean() {
use = false;
}
public boolean getUse() {
return use;
}
public String getTytul() {
return tytul;
}
public void setTytul(String tytul) {
this.tytul = tytul;
}
public String getOpis() {
return opis;
}
public void setOpis(String opis) {
this.opis = opis;
}
public int getCode() {
return CODE;
}
private void add() {
use = true;
}
public void addBean() {
add();
pdf.addElement(this);
System.out.println("InnePanel after pdf.addElement() this.opis:" + this.opis);
// This sysout prints the correct value after give object to factory
}
}
Factory piece of code:
public int addElement(PdfElement element) {
pdfType = true;
if (element.getUse()) {
elementList.add(element);
return 1;
}
return 0;
}
public void prepare() {
for (PdfElement element : elementList) {
System.out.println("element.code:" + element.getCode());
switch (element.getCode()) {
case 0:
if (nF != null)
break;
nF = new NaglowekFactory(element, Counter.getNumber());
break;
case 1:
if (pF != null)
break;
pF = new ProduktyFactory(element, Counter.getNumber());
prodSum = pF.getProdukty().getSuma();
euroData = pF.getProdukty().getEuroData();
break;
case 2:
if (mF != null)
break;
mF = new MontazFactory(element, Counter.getNumber());
servSum = mF.getMontaz().getSuma();
break;
case 3:
if (uF != null)
break;
uF = new UslugiFactory(element, Counter.getNumber());
asmSum = uF.getUslugi().getSuma();
break;
case 4:
if (iF != null)
break;
iF = new InneFactory(element, Counter.getNumber());
//here Opis value is empty
break;
}
}
System.out.println("factory.prepare() ->");
}
Constructor of InneFactory:
PanelInneBean inne;
public InneFactory(PdfElement element, int order) {
inne = (PanelInneBean) element;
System.out.println("innerFactory constructor, inne.getTytul(): "
+ inne.getTytul());
//here values are empty
this.order = order;
list = new ArrayList<Element>();
}
public int getOrder() {
return order;
}
what I'm doing wrong?
I think you are running into a common misconception about how #ViewScoped beans really work. The bean object itself is created on every Faces request and destroyed after every Faces response.
After the bean is re created however JSF will enter the first lifecycle phase RestoreView. During this phase it will fetch the last good values for this bean from the View State and apply them to the beans Managed Properties.
Basically what is happening is that you are invoking an action, which generates a seperate request than the one that loaded the page, and at this point the reference you added to the PdfFactory is now lost because the PdfFactory in the last object was destroyed!
The best way to resolve this would be to make PdfFactory a Managed Bean. You can initialize the factory in the #PostConstruct method and you can inject other Managed Beans into your PdfFactory bean using the #ManagedProperty annotation.
#ManagedBean
#RequestScoped
public PdfFactory extends ... implements Serializable {
#ManagedProperty("#{myViewBean}")
private MyViewBean viewBean;
#PostConstruct
public initializeFactory() {
//Do stuff
}
public void prepare() {
// prepare stuff
}
}
Related
I have a class with,
a field called something,
a setter method called setSomething, and,
a method called onChange which should be called every time something is changed.
I want to be able to freely add more fields and have the same behavior for all of them.
I don't want to manually call onChange because,
A lot of boilerplate,
Code will be written in Kotlin so I don't want to write setter functions at all.
The ideal solution I've been able to think of has been to somehow inject the onChange call right before the return for each setter method in compile time.
I've looked at annotation processing, but apparently classes aren't actually compiled at that stage, so I'd have to generate the entire class all over again? I don't exactly understand this.
The other option seems to be writing a gradle plugin that will find the relevant class(es) and modify their bytecode.
I've actually started work on this as a pure Java project (gradle plugin is semi-done) and have been able to find the classes and inject the method call. Can't seem to successfully write the results to a class file though.
Here's what I have (using BCEL):
public class StateStoreInjector {
public static void main(String[] args) {
// Find all classes that extends StateStore
Reflections reflections = new Reflections("tr.xip.statestore");
Set<Class<? extends StateStore>> classes = reflections.getSubTypesOf(StateStore.class);
for (Class c : classes) {
try {
JavaClass clazz = Repository.lookupClass(c.getName());
JavaClass superClazz = Repository.lookupClass(StateStore.class.getName());
if (Repository.instanceOf(clazz, superClazz)) {
injectInClass(clazz, superClazz);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
private static void injectInClass(JavaClass clazz, JavaClass superClazz) {
ClassGen classGen = new ClassGen(clazz);
ConstantPoolGen cp = classGen.getConstantPool();
// Find the onChange method
Method onChangeMethod = null;
for (Method m : superClazz.getMethods()) {
if (m.getName().equals("onChange")) {
onChangeMethod = m;
}
}
if (onChangeMethod == null) {
throw new RuntimeException("onChange method not found");
}
ClassGen superClassGen = new ClassGen(superClazz);
ConstantPoolGen superCp = superClassGen.getConstantPool();
// Add onChange method ref to the class ConstantPool
MethodGen onChangeMethodGen = new MethodGen(onChangeMethod, superClassGen.getClassName(), superCp);
cp.addMethodref(onChangeMethodGen);
// Loop through all methods to inject method invocations if applicable
for (Method m : clazz.getMethods()) {
// Skip methods with names shorter than 3 chars - we're looking for setters and setters would be min 4 chars
if (m.getName().length() < 3) continue;
// Check if the method actually starts with the keyword "set"
boolean isSetMethod = m.getName().substring(0, 3).toUpperCase().equals("SET");
// Get method name without the "set" keyword
String methodName = m.getName().substring(3, m.getName().length());
// Check that we actually have a field set by this setter - that this setter is "valid"
boolean fieldWithSameNameExists = false;
for (Field f : clazz.getFields()) {
if (f.getName().toUpperCase().equals(methodName.toUpperCase())) {
fieldWithSameNameExists = true;
break;
}
}
// Proceed with injection if criteria match
Method newMethod = null;
if (isSetMethod && fieldWithSameNameExists) {
newMethod = injectInMethod(m, onChangeMethodGen, classGen, cp);
}
// Injection returned. Do we have a new/modified method? Yes? Update and write class.
if (newMethod != null) {
classGen.removeMethod(m);
classGen.addMethod(newMethod);
classGen.update();
try {
String packageName = clazz.getPackageName().replace(".", "/");
String className = clazz.getClassName();
className = className.substring(className.lastIndexOf(".") + 1, className.length());
clazz.dump(packageName + "/" + className + "Edited.class");
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static Method injectInMethod(Method m, MethodGen onChangeMethodGen, ClassGen cg, ConstantPoolGen cp) {
MethodGen methodGen = new MethodGen(m, cg.getClassName(), cp);
InstructionList il = methodGen.getInstructionList();
println(il.toString() + "pre insert ^");
// Find the "return" instruction
Instruction returnInstruction = null;
for (Instruction i : il.getInstructions()) {
if (i.getOpcode() == 177) returnInstruction = i;
}
// If found, insert onChange invocation instruction before the return instruction
if (returnInstruction != null) {
int index = cp.lookupMethodref(onChangeMethodGen); // Find the index of the onChange method in the CP
il.insert(returnInstruction, new INVOKEVIRTUAL(index)); // Insert the new instruction
println(il.toString() + "post insert ^");
il.setPositions(); // Fix positions
println(il.toString() + "post set pos ^");
il.update();
methodGen.update();
return methodGen.getMethod();
}
return null;
}
private static void println(String message) {
System.out.println(message);
}
}
Input Java class:
public class DummyStateStore extends StateStore {
private int id = 4321;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
Parent Store class:
public class StateStore {
public void onChange() {
// notifies all subscribers
}
}
Output (decompiled) class file:
public class DummyStateStore extends StateStore {
private int id = 4321;
public DummyStateStore() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
}
Log output:
0: aload_0[42](1)
1: iload_1[27](1)
2: putfield[181](3) 2
5: return[177](1)
pre insert ^
0: aload_0[42](1)
1: iload_1[27](1)
2: putfield[181](3) 2
-1: invokevirtual[182](3) 26
5: return[177](1)
post insert ^
0: aload_0[42](1)
1: iload_1[27](1)
2: putfield[181](3) 2
5: invokevirtual[182](3) 26
8: return[177](1)
post set pos ^
(I checked the index 26 by debugging the code and it is the correct index in the CP)
Now, the questions are:
Why can't the invocation be seen in the decompiled code but it seems to be added to the instructions list? What am I missing?
Where would I be exporting the modified class files in an android build for them to be included in the final apk?
You're trying to use reflection, but there should be no need to do so with Kotlin as you can create higher order functions (functions that take functions as inputs).
You could do something like:
class ChangeableType<T>(private var value: T, private val onChange: () -> Unit) {
fun set(value: T) {
this.value = value
this.onChange.invoke()
}
}
class MyRandomClass() {
val something = ChangeableType(0, { System.print("Something new value: $value") })
val anotherThing = ChangeableType("String", { System.print("Another thing new value: $value") })
}
class ConsumingClass {
val myRandomClass = MyRandomClass()
fun update() {
myRandomClass.apply {
something.set(1)
anotherThing.set("Hello World")
}
}
}
Still struggling with properly making a cacheBean. I think I want the bean to be a singleton, from what I have read. Will only need
one instance of it. Use it to get often used keywords and so on.
http://blog.defrog.nl/2013/02/prefered-way-for-referencing-beans-from.html
I used this pattern to make my CacheBean (and used a utility method).
If I make this a managedBean by putting it into Faces-config, then I can easily get the value of models
<xp:text escape="true" id="computedField1"
value="#{CacheBean.models}"></xp:text>
The JSF takes care of instantiating the bean for me.
But I don't want it to reload the same values (like models) over and over. I thought that to get that to happen I needed to make
a POJO and grab the currentInstance of the bean, as in the url.
However, when I made this change (taking the bean out of the faces-config file, I cannot seem to get a handle on the properties.
This won't even compile:
<xp:text escape="true" id="computedField1"
value="#{Cache.getCurrentInstance().models}">
</xp:text>
What am I doing wrong?
================================
package com.scoular.cache;
import java.io.Serializable;
import org.openntf.domino.xsp.XspOpenLogUtil;
import com.scoular.Utils;
public class CacheBean implements Serializable {
private static final long serialVersionUID = -2665922853615670023L;
public static final String BEAN_NAME = "CacheBean";
private String pcDataDBpath;
private Vector<Object> models = new Vector<Object>();
public CacheBean() {
initConfigData();
}
private void initConfigData() {
try {
loadModels();
loadDBPaths();
} catch (Exception e) {
XspOpenLogUtil.logError(e);
}
}
// Getters and Setters
public static CacheBean getInstance(String beanName) {
return (CacheBean) Utils.getVariableValue(beanName);
}
public static CacheBean getInstance() {
return getInstance(BEAN_NAME);
}
public String getPcDataDBpath() {
return pcDataDBpath;
}
public void setPcDataDBpath(String pcDataDBpath) {
this.pcDataDBpath = pcDataDBpath;
}
public void loadDBPaths() {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
pcAppDBpath = (tmpDB.getServer() + "!!" + "scoApps\\PC\\PCApp.nsf");
pcDataDBpath = (tmpDB.getServer() + "!!" + "scoApps\\PC\\PCData.nsf");
compDirDBpath = (tmpDB.getServer() + "!!" + "compdir.nsf");
}
public void loadModels() {
try {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
Database PCDataDB = session.getDatabase(tmpDB.getServer(), "scoApps\\PC\\PCData.nsf");
ViewNavigator vn = PCDataDB.getView("dbLookupModels").createViewNav();
ViewEntry entry = vn.getFirst();
while (entry != null) {
Vector<Object> thisCat = entry.getColumnValues();
if (entry.isCategory()) {
String thisCatString = thisCat.elementAt(0).toString();
models.addElement(thisCatString);
}
entry = vn.getNextCategory();
}
} catch (Exception e) {
XspOpenLogUtil.logError(e);
}
}
p
ackage com.scoular;
import javax.faces.context.FacesContext;
public class Utils {
public static Object getVariableValue(String varName) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getVariableResolver().resolveVariable(context, varName);
}
}
When the bean has the right scope you can access the bean directly if is created.
private static final String BEAN_NAME = "CacheBean";
//access to the bean
public static CacheBean get() {
return (CacheBean) JSFUtil.resolveVariable(BEAN_NAME);
}
//in my JSFUtil class I have the method
public static Object resolveVariable(String variable) {
return FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), variable);
}
so in a Java Class you can call
CacheBean.get().models
in EL you can use
CacheBean.models
I can tell you why it's not compiling at least.
value="#{Cache.getCurrentInstance().models}"
That's EL. So there should not be a get or a (). You want
value="#{Cache.currentInstance.models}"
And check your var name as I thought you were using CacheBean and not Cache.
I have a following code
public class Component extend Framework {
private Integer someInt;
private String someString;
public Integer getSomeInt() {
return someInt;
}
public String getSomeString() {
return someString;
}
public void activate() {
Integer tempInt = (Integer)getProperties("key"); // From Framework
if (tempInt == null) {
tempInt = (Integer)getRequest().getProperties("key"); // From Framework
}
if(tempInt == null)
tempInt = (Integer)getBind().getProperties("key"); // From Frameowrk
someString = makeServiceCall("http://.....?key=tempInt");
}
}
Basically activate() method is called by the framework in order to access internal state of the framework to construct Component object. activate() is sort of like a setter for the Component object.
If I were to unit test the code above, what would be the best way to do it without having to have framework running?
One way would be to mock out Component class and stub the super.getProperties... calls, however if we mock the class in question, what is the point of testing to begin with?
I will show how to test one edge case
void testServiceCallWithNoKeyPropertyFound() {
Component componentUnderTest = new Component() {
Integer getProperties(String key) {
return null; // property should not be found
}
Request getRequest() {
return new Request(...); //this request should not contain a property named "key",
}
Bind getBind() {
return new Bind(...); //this bind should not contain a property named "key"
}
String makeServiceCall(String url) {
if (url.endsWith("null")) {
return success;
}
throw new AssertionError("expected url ending with null, but was " + url);
}
};
componentUnderTest.activate();
assertThat(componentUnderTest.getSomeString(), equalTo("success"));
}
Using Mockito (spys) can make this example much more concise. But this would hide the principles how to design the test.
There are some more edge cases:
void testServiceCallWithPropertyFoundInComponent() ...
void testServiceCallWithPropertyFoundInRequest() ...
void testServiceCallWithPropertyFoundInBind() ...
Use Mockito.
Spy the Component class and mock the methods getRequest() and getBind().
Finally, call the activate() method directly from your unit test.
I think it could be a smell of bad design. Maybe you should consider composition instead of inheritance? It would be more testing friendly and more objective. Why Component is inheriting from Framework class?
public class Component {
private int someInt;
private String someString;
private Framework framework;
public Component(Framework framework) {
this.framework = framework
}
public int getSomeInt() {
return someInt;
}
public String getSomeString() {
return someString;
}
public void activate() {
int tempInt = framework.getProperties("key"); // From Framework
if (tempInt == null) {
tempInt = framework.getRequest().getProperties("key"); // From Framework
}
if(tempInt == null)
tempInt = framework.getBind().getProperties("key"); // From Frameowrk
someString = makeServiceCall("http://.....?key=tempInt");
}
}
I have the problem that I want to and need to get rid of some if else cases. I got the following code in my project:
if (ar[4].equals("week")) {
WeekThreshold wt = new WeekThreshold();
firstTime = unparsedDate.format(wt.getStartDate().getTime());
secondTime = unparsedDate.format(wt.getEndDate().getTime());
} else if (ar[4].equals("month")) {
MonthThreshold mt = new MonthThreshold();
firstTime = unparsedDate.format(mt.getStartDate().getTime());
secondTime = unparsedDate.format(mt.getEndDate().getTime());
} else if (ar[4].equals("quarter")) {
quarterThreshold();
} else if (ar[4].equals("year")) {
YearThreshold yt = new YearThreshold();
firstTime = unparsedDate.format(yt.getStartDate().getTime());
secondTime = unparsedDate.format(yt.getEndDate().getTime());
}
That three classes WeekThreshold, MonthThreshold and YearThreshold extend from an AbstractThreshold class where they get dates from a calendar, but that is not important. The method quarterThreshold() is special and can stay there. But how can I get rid of that if else blocks and have one statement to call different classes?
EDIT: Forgot to mention, the classes that need to be called are from a variety of the array ar[]. If the array ar[4] is month, MonthThreshold must be called, etc.
Multiple possibilities... Do the XYZThreshold classes have a common interface, like Threshold? Then you could assign a variable with that, for example...
Threshold threshold = null;
if ((ar[4].equals("week")) {
threshold = new WeekThreshold();
} else ... {
}
firstTime = unparsedDate.format(threshold.getStartDate().getTime());
secondTime = unparsedDate.format(threshold.getEndDate().getTime());
That would be a first step. If you wanted, you could, for example, use an enum to store your Thresholds:
enum Thresholds {
WEEK("week") {
public Threshold getThreshold() {
return new WeekThreshold();
}
},
etc.
private String period;
private Thresholds(String period) {
this.period = period;
}
public abstract Threshold getThreshold();
// ...add a static class to iterate and search by period,
// ...so you can write Threshold threshold = Thresholds.getByPeriod("week").getThreshold();
}
Using enums is a personal taste, of course, you can do the same thing with normal classes or by simply putting your if-block for the Threshold-choosing into a seperate class.
You can merge the common code (unparsedDate.format(...)) outside like this:
AbstractThreshold at = null;
switch(ar[4]) {
case "week":
at = new WeekThreshold();
break;
case "month":
at = new MonthThreshold();
break;
case "year":
at = new YearThreshold();
break;
case "quarter":
quarterThreshold();
break;
}
if(at != null) {
firstTime = unparsedDate.format(at.getStartDate().getTime());
secondTime = unparsedDate.format(at.getEndDate().getTime());
}
Of course an overengineered version is possible. Here's just an illustration how it can be implemented using the Java-8 features:
// Map can be initialized only once, then used many times
Map<String, Supplier<AbstractThreshold>> thresholdSuppliers = new HashMap<>();
thresholdSuppliers.put("week", WeekThreshold::new);
thresholdSuppliers.put("month", MonthThreshold::new);
thresholdSuppliers.put("year", YearThreshold::new);
AbstractThreshold at = thresholdSuppliers.getOrDefault(ar[4], () -> null).get();
if(at != null) {
firstTime = unparsedDate.format(at.getStartDate().getTime());
secondTime = unparsedDate.format(at.getEndDate().getTime());
} else if(ar[4].equals("quarter"))
quarterThreshold();
}
Here you can make good use of the FactoryPattern
class ThresholdFactory
{
public static AbstractThreshold getThreshold(String criteria)
{
if ( criteria.equals("week") )
return new WeekThreshold();
if ( criteria.equals("month") )
return new MonthThreshold();
if ( criteria.equals("year") )
return new YearThreshold();
return null;
}
}
The rest of the code looks then like this:
AbstractThreshold at = ThresholdFactory.getThreshold(ar[4]);
if(at != null){
firstTime = unparsedDate.format(at.getStartDate().getTime());
secondTime = unparsedDate.format(at.getEndDate().getTime());
} else {
quarterThreshold();
}
first create threshold factory,
static enum ThresholdsFactory {
week(new WeekThreshold()), month(new MonthThreshold())/* etc */;
static private Map<String,ThresholdsFactory> lookup = new HashMap<String, ThresholdsFactory>();
static{
for(ThresholdsFactory val : ThresholdsFactory.values()){
lookup.put(val.name(), val);
}
}
public AbstractThreshold threshold;
public static ThresholdsFactory find(String name){
return lookup.get(name);
}
ThresholdsFactory(AbstractThreshold th) {
threshold = th;
}
}
now all what you need to do is
AbstractThreshold th = ThresholdsFactory.find(ar[4]);
if (th!=null){
firstTime = unparsedDate.format(th.getStartDate().getTime());
secondTime = unparsedDate.format(th.getEndDate().getTime());
}
Here is an example of how to use interfaces and Factory design pattern
If your multiple implementors share common code, have them all extend an Abstract class that implements the interface. It is a good idea to refer to your methods through the interface, not the concrete class to take advantage of polymorphism ... see code below ...
public class Example {
public static void main(String[] args) {
String[] intervals = {"week", "week", "quarter", "month", "year", "week"};
IThreshold[] objects = new IThreshold[intervals.length];
// Create your objects using Factory pattern
for(int index = 0; index < intervals.length; index++) {
objects[index] = ThresholdFactory.createInstance(intervals[index]);
}
// Now iterate through your objects and refer to them through a common interface
for(IThreshold object : objects) {
int start = object.getFirstTime();
int end = object.getFirstTime();
}
}
}
interface IThreshold {
public int getFirstTime();
public int getLastTime();
}
abstract class AbstractThreshold implements IThreshold {
#Override
public int getFirstTime() {
// TODO Auto-generated method stub
return 0;
}
#Override
public int getLastTime() {
// TODO Auto-generated method stub
return 0;
}
}
class WeekThreshold extends AbstractThreshold {}
class MonthThreshold extends AbstractThreshold {}
class QuarterThreshold extends AbstractThreshold {}
class YearThreshold extends AbstractThreshold {}
class ThresholdFactory {
public static final IThreshold createInstance(String interval) {
IThreshold instance = null;
if(interval.equals("week")){
instance = new WeekThreshold();
}
else if(interval.equals("month")){
instance = new MonthThreshold();
}
else if(interval.equals("quarter")){
instance = new QuarterThreshold();
}
else {
if(interval.equals("year")){
instance = new YearThreshold();
}
}
return instance;
}
}
You could use a switch statement
String typeOfDay;
switch (dayOfWeekArg) {
case "Monday":
typeOfDay = "Start of work week";
break;
case "Tuesday":
case "Wednesday":
case "Thursday":
typeOfDay = "Midweek";
break;
case "Friday":
typeOfDay = "End of work week";
break;
case "Saturday":
case "Sunday":
typeOfDay = "Weekend";
break;
default:
throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
}
You can replace the example that I stole from java documentation, with your own code
switch(periodType){
case "week":
WeekThreshold wt = new WeekThreshold();
break; // add your other cases
}
firstTime = unparsedDate.format(wt.getStartDate().getTime());
secondTime = unparsedDate.format(wt.getEndDate().getTime());
I have a web service which returns a list of objects of an enumerated type. The enum has been annotated with #XmlRootElement, as well as another wrapper type which contains a list of these enumerated type objects plus another string member.
#XmlRootElement(name = "indicator")
public enum IndicatorEnum
{
FROST_DAYS,
ICING_DAYS,
MAX_TEMPS_MUCH_ABOVE_AVG,
UNKNOWN;
public static IndicatorEnum fromInt(final int value)
{
switch (value)
{
case 1:
return FROST_DAYS;
case 2:
return ICING_DAYS;
case 3:
return MAX_TEMPS_MUCH_ABOVE_AVG;
default:
return UNKNOWN;
}
}
public static IndicatorEnum fromString(final String dataType)
{
if ("FROST_DAYS".equals(dataType))
{
return FROST_DAYS;
}
else if ("ICING_DAYS".equals(dataType))
{
return ICING_DAYS;
}
else if ("MAX_TEMPS_MUCH_ABOVE_AVG".equals(dataType))
{
return MAX_TEMPS_MUCH_ABOVE_AVG;
}
return UNKNOWN;
}
public String getValueUnits()
{
switch (this)
{
case FROST_DAYS:
return "days";
case ICING_DAYS:
return "days";
case MAX_TEMPS_MUCH_ABOVE_AVG:
return "percentages";
default:
return "UNKNOWN VALUE UNITS";
}
}
public String toDisplayString()
{
switch (this)
{
case FROST_DAYS:
return "Frost Days";
case ICING_DAYS:
return "Icing Days";
case MAX_TEMPS_MUCH_ABOVE_AVG:
return "Max Temps Much Above Average";
default:
return "UNKNOWN";
}
}
#Override
public String toString()
{
switch (this)
{
case FROST_DAYS:
return "FROST_DAYS";
case ICING_DAYS:
return "ICING_DAYS";
case MAX_TEMPS_MUCH_ABOVE_AVG:
return "MAX_TEMPS_MUCH_ABOVE_AVG";
default:
return "UNKNOWN";
}
}
}
#XmlRootElement(name = "available_indicators_for_station")
public class AvailableIndicatorsForStationBean
{
private List<IndicatorEnum> availableIndicators;
private String stationCode;
/**
* Default, no-arg constructor.
*/
public AvailableIndicatorsForStationBean()
{
}
/**
* Constructor.
*
* #param stationCode
* #param availableIndicators
*/
public AvailableIndicatorsForStationBean(final String stationCode,
final List<IndicatorEnum> availableIndicators)
{
this.stationCode = stationCode;
this.availableIndicators = availableIndicators;
}
#XmlElement(name = "available_indicators")
public List<IndicatorEnum> getAvailableIndicators()
{
return availableIndicators;
}
#XmlElement(name = "station_code")
public String getStationCode()
{
return stationCode;
}
public void setAvailableIndicators(final List<IndicatorEnum> availableIndicators)
{
this.availableIndicators = availableIndicators;
}
public void setStationCode(final String stationCode)
{
this.stationCode = stationCode;
}
}
I have a controller class which returns a model and view, like so:
#RequestMapping(method = RequestMethod.GET, value = "/available_indicators_for_station_xml")
public ModelAndView getAvailableIndicatorsXml(#RequestParam("station_code") final String stationCode)
{
// validate the parameters
if ((stationCode == null) || stationCode.isEmpty())
{
throw new RuntimeException("Missing required request parameter: \'station_code\'");
}
// find the matching list of Observations entities
List<IndicatorEnum> availableIndicators = stationDao.findAvailableIndicatorsForStation(stationCode);
// convert the list of indicators to a JAXB bindable model object
AvailableIndicatorsForStationBean availableIndicatorsForStationBean = new AvailableIndicatorsForStationBean(stationCode,
availableIndicators);
// pass it on as a model and view
return new ModelAndView(jaxb2MarshallingView, "available_indicators_for_station", availableIndicatorsForStationBean);
}
When I make a request to the web service I get an error with the JAXB marshalling:
javax.servlet.ServletException: Unable to locate object to be marshalled in model: {org.springframework.validation.BindingResult.available_indicators_for_station=org.springframework.validation.BeanPropertyBindingResult: 0 errors, available_indicators_for_station=com.abc.rest.model.AvailableIndicatorsForStationBean#9eb530}
org.springframework.web.servlet.view.xml.MarshallingView.renderMergedOutputModel(MarshallingView.java:100)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1063)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
Can anyone advise as to what I am doing wrong? Thanks in advance for any suggestions.
I encountered this exception, javax.servlet.ServletException: Unable to locate object to be marshalled in model, because the object I had placed in the model had public members which I did not want serialised but which I had not annotated as #XmlTransient