Uploading Files tutorial - spring

I'm learning this tutorial: https://spring.io/guides/gs/uploading-files/
In my IDE StorageProperties is underlined red. But in the tutorial, if I'm not mistaken there is no file StorageProperties. Could you tell me whether it is possible to follow this tutorial or it is practically impossible?

StorageProperties.java is present here https://github.com/spring-guides/gs-uploading-files/blob/main/complete/src/main/java/com/example/uploadingfiles/storage/StorageProperties.java
#ConfigurationProperties("storage")
public class StorageProperties {
/**
* Folder location for storing files
*/
private String location = "upload-dir";
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
I hope this solves your query.

Related

MOXyJsonProvider not working

In my REST applications (under GlassFish 4.1.2) I want to convert POJOs to JSON and back again. The examples all make it look easy but I am missing something.
Here is my application:
#ApplicationPath("/")
public class RootApp extends Application {
#Override
public Set<Class<?>> getClasses() {
HashSet set = new HashSet<Class<?>>();
set.add(HelloWorld.class);
return set;
}
#Override
public Set<Object> getSingletons() {
HashSet set = new HashSet<Object>();
MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();
moxyJsonProvider.setFormattedOutput(true);
set.add(moxyJsonProvider);
return set;
}
}
And here is the Resource:
#Path("helloworld")
public class HelloWorld {
private static int counter = 1;
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response getInevitableMessage() {
JsonHello hj = new JsonHello("Hello World", counter++);
return Response.ok(hj).build();
}
}
And last and least is the POJO to convert to and from JSON:
public class JsonHello {
private int count;
private String message;
public JsonHello(String message, int count) {
this.message = message;
this.count = count;
}
public int count() { return count; }
public void count(int value) { count = value; }
public String message() { return message; }
public void message(String value) { message = value; }
}
I am referring to the tagged answer in this thread. When I attempt to access "/helloworld" it pitches the following exception:
org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
This application works if the resource just returns a string. There is nothing in the web.xml file since I am letting Glassfish set the application via its decorators.
Any idea what I am missing here?
I ended up solving the problem using the direction that #peeskillet suggested. MOXyJsonProvider is unneeded.
One problem that is hard to address is that almost all the examples on the web assume you are configuring your Servlet with a web.xml file, which I am not. All the configuration I do is from inside the Application object. The Jersey documentation does not make this very clear. What ends up working is this:
#Override
public Set<Class<?>> getClasses() {
HashSet set = new HashSet<Class<?>>();
set.add(JacksonFeature.class);
set.add(MyObjectMapperProvider.class);
set.add(Home.class);
set.add(HelloWorld.class);
return set;
}
At this point the REST resources can produce and consume various POJOs which are transcoded into JSON perfectly and without any effort.
Instead of just deleting this question I will put this answer here in hopes of saving someone the amount of time I spent finding this out.

"Public documented API" broken in Sonar?

I'm running Sonar 5.0 with the Java 2.8 plugin. It generates a percentage value for public documented API, which is extremely low.
This is caused by having entity classes like the following being marked as 0% being documented:
public class SomeEntity {
/** Some Javadoc */
private String someField;
public String getSomeField() { return someField; }
public void setSomeField(String someField) { this.someField = someField; }
}
Based on https://dev.eclipse.org/sonar/rules/show/squid:UndocumentedApi?layout=false I'd argue that getters and setters should be ignored. Is there some magic trick to convince sonarqube to no longer warn about these?
The only element that is public in your example (the SomeEntity class) is not documented at all, so that might explain why your "Public documented API" measure is very low.
There is one comment missing for the class
/** COMMENT YOUR CLASS **/
public class SomeEntity {
/** Some Javadoc */
private String someField;
public String getSomeField() { return someField; }
public void setSomeField(String someField) { this.someField = someField; }
}

Mixed Entity and Business classes - Refactor help needed

I have a project where Entity Classes and Business classes are mixed up. The entity beans are part of the business and all is used through the whole project.
How can I best refactor those classes to separate those layers. I also want to keep the changes to the implementers as minimal as possible. Preferable no changes, otherwise hundreds of references need to be updated.
How should I rename the classes and work through this?
Example of mixed code:
// Mixed business-entity class
public final class Language {
private final Long id;
private final String code;
private final String description;
//Constructor
public Language() {
}
//getters and setters
public String getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
...
//Business is a part of this class
public static Language findByUser(User user) {
Language language;
...implementation to find user language...
return language;
}
....
}
// Implementing class
public class Messenger {
public Messenger() {
}
public static void sendEmail() {
...
Language emailLanguage = Language.findByUser(user):
...
}
}
I want to separte those layers in:
// Entity Class
public final class Language {
private final Long id;
private final String code;
private final String description;
//Constructor
public Language() {
}
//getters and setters
public String getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
...
}
// Business Class
public final class LanguageImpl {
public LanguageImpl() {
}
public static Language findByUser(User user) {
Language language;
...implementation to find user language...
return language;
}
....
}
Provide minimal changes to implementation classes, preferable no changes. Otherwise a lot of work will come because of the references all over the code-base.
// Implementing class
public class Messenger {
public Messenger() {
}
public static void sendEmail() {
...
Language emailLanguage = Language.findByUser(user);
...
}
}
How do I work through this refactoring?
How should I rename my classes?
Any thoughts would be very helpful! Thanks!
This is my solution. Please review and accept this if it looks good. Thanks!
The mixed business-entity class is re-used as a wrapper class. This makes it possible to re-use this in all implementing classes where no changes are needed.
public final class Language Extends LanguageImpl{
private final LanguageEntity languageEntity;
//Constructor
public Language(LanguageEntity le) {
languageEntity = le;
}
//Wrapper method
public static Language findByUser(User user) {
LanguageEntity le = findEntityByUser(user);
Language language = new Language(le);
return language;
}
....
}
A new Entity class is created (LanguageEntity) in a new package. This avoids package and naming conflicts with the original mixed class (Language). All entity fields and methods from the mixed class are moved here.
package com.test.entity;
public final class LanguageEntity {
private final Long id;
private final String code;
private final String description;
//Constructor
public LanguageEntity() { }
//getters and setters
public String getId() { return this.id; }
public void setId(Long id) { this.id = id; }
...
}
A new business class is created (LanguageImpl) in a new package. All business methods are moved here. The original mixed class will extend this new business class.
package com.test.impl
public final class LanguageImpl {
//Constructor
public LanguageImpl() { }
//Business is a part of this class
public static LanguageEntity findEntityByUser(User user) {
LanguageEntity language;
...implementation to find user language...
return language;
}
....
}
This is an implementing class that does not need changes. Hundreds of implementation locations remain unchanged, which saves a lot of work. Hurray!
public class Messenger {
public Messenger() { }
public static void sendEmail() {
...
Language emailLanguage = Language.findByUser(user):
...
}
}
And for future development, the new combination LanguageEntity and LanguageImpl will be used. The original Language will be deprecated.
Please leave comments on this solution. Other solutions are more than welcome!

Wicket: Label with default text if model empty + CompoundPropertyModel

I have:
public FooPage( ... ) {
this.setDefaultModel( new CompoundPropertyModel(new GenericIdLDM( Foo.class, 1)) );
add(new Label("title"));
I'd like to have $subj.
I've found this solution from 2007 (point below) : http://www.mail-archive.com/wicket-user#lists.sourceforge.net/msg29603.html
However, it wouldn't work for CPM as it needs the constructor with model.
How could I make it work with CPM?
public class DefaultTextModel extends AbstractReadOnlyModel<String> {
private final IModel<String> delegate;
private final String def;
public DefaultTextModel(String def, IModel delegate) {
this.def = def;
this.delegate = delegate;
}
public String getObject() {
String s = delegate.getObject();
return (Strings.isEmpty(s)) ? def : s;
}
public void detach() {
delegate.detach();
}
}
You could have a custom Converter for your label. I think the better reflects your intentions as well. See for example https://cwiki.apache.org/WICKET/using-custom-converters.html#Usingcustomconverters-InWicket1.4
Other option could be JavaScript, check if the span is empty and then provide the default value.
I can override Label#initModel():
protected IModel<?> initModel() {
return new DefaultTextModel(defaultModel, super.initModel());
}
A simpler solution is to override Label#onComponentTagBody() and just apply the default text there.
(Sven Meier replied on the mailing list)

Autocomplete using advanced model in wicket

First I want to say I saw a site:
https://cwiki.apache.org/WICKET/autocomplete-using-a-wicket-model.html
Still I have some problem with implementation.
Context
I have form where want to edit Order from my database.
I want to autocomplete client's name in form and save it to database.
Problem
I can see suggestions of client names and I can choose which client's name I'll use.
Just when I submit form new client won't be saved in order, in database.
I don't know how to relate form Model with AutoCompleteTextField input.
Any ideas ?
Pseudo code of my classes:
Order{
Long id;
String date;
Client client;
Status status;
...
}
Client{
Long id;
String name;
String nip;
String address;
String postcode;
String city;
String phone;
String mail;
...
}
Status{
Long id;
String name;
String value;
}
Edited:
Yes you are right.
My implementation of AbstractAutoCompleteTextField from site:
AbstractAutoCompleteRenderer autoCompleteRenderer = new AbstractAutoCompleteRenderer() {
protected final String getTextValue(final Object object) {
Client client = (Client) object;
return client.getName();
}
protected final void renderChoice(final Object object, final Response response, final String criteria) {
response.write(getTextValue(object));
}
};
// textfield
AbstractAutoCompleteTextField<Client> name = new AbstractAutoCompleteTextField<Client>("name", new PropertyModel(order, "client"), autoCompleteRenderer) {
protected final List<Client> getChoiceList(final String input) {
return clientService.findByNames(10, 0, input);
}
protected final String getChoiceValue(final Client choice) throws Throwable {
return choice.getId().toString();
}
};
form.add(name);
My form implementation you asked for:
form = new Form("orderForm", new CompoundPropertyModel(order)) {
#Override
public void onSubmit() {
orderService.update((Order) getDefaultModelObject());
setResponsePage(OrdersPage.class);
// Form validation successful. Display message showing edited
// model.
}
};
With this code I got: "'Hurtownia Techniczna "ADA"' is not proper Client." (translated from Polish) in feedback panel.
I think, that's where things go wrong:
AbstractAutoCompleteTextField<Client> name = new AbstractAutoCompleteTextField<Client>("name", new PropertyModel(order, "client"), autoCompleteRenderer)
Rest of the answer edited to reflect the correct use case
To be specific: You're creating a PropertyModel of your orders client value, which is a Client-Object and tie it to a TextField. To create a Client-object from the TextField, wicket needs a converter. There are lots of build-in converters but none of them is capable of converting to a custom object. Thus you'll need to provide the converter by implementing IConverter. Since I don't know how you store and retrieve your Client-objects I can only show you a generic example, a Locale-Converter used in one of my projects:
public class LocaleConverter implements IConverter {
private static final long serialVersionUID = 3251433094703013493L;
/* (non-Javadoc)
* #see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String, java.util.Locale)
*/
#Override
public Object convertToObject(String value, Locale locale) {
Locale retValue = null;
try {
retValue = LocaleUtils.toLocale(value);
} catch (IllegalArgumentException e) {
throw (new ConversionException("" + value + " is not a valid locale.", e));
}
return retValue;
}
/* (non-Javadoc)
* #see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object, java.util.Locale)
*/
#Override
public String convertToString(Object value, Locale locale) {
return value.toString();
}
}
Then you'll need to register your new converter to your application. Again, this will be slightly different for you since you've got a different converter...
#Override
protected IConverterLocator newConverterLocator() {
ConverterLocator newConverterLocator = (ConverterLocator) super.newConverterLocator();
newConverterLocator.set(Locale.class, new LocaleConverter());
return newConverterLocator;
}
This method needs to be included in your application class.

Resources