Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException - spring-mvc

I a trying to send object from client to server. Below is the Controller using spring mvc.
#Controller
public class HomeController {
public class User{
public String name;
public String email;
public User() {
super();
}
public User(String name, String email) {
super();
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
}
#RequestMapping(value="/getUsers", method= RequestMethod.POST)
public #ResponseBody User getUser(User user){
return user;
}}
In client side I am sending object in post like this
http://localhost:2015/spring/getUsers
POST /spring/getUsers HTTP/1.1
Host: localhost:2015
Cache-Control: no-cache
{"name":"vinod", "email":"vinod#gmaol.com" }
But I am getting error at server side like this
HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.vinod.spring.HomeController$User]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.vinod.spring.HomeController$User.<init>()

Try make the refactoring for your User class to another file, and put a reference of it in your controller using the #Resource annotation. Maybe the exception was throwed because User class is nested.
Example:
User.java
#Component
public class User{
public String name;
public String email;
public User() {
super();
}
public User(String name, String email) {
super();
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
}
HomeController.java
#Controller
public class HomeController {
#Resource
User user;
#RequestMapping(value="/getUsers", method= RequestMethod.POST)
public #ResponseBody User getUser(User user){
return user;
}
}

Related

Asp.Net webapi : throw new RestException(HttpStatusCode.Unauthorized)

I am a beginner developer and I have an issue in Query handler when using throw new RestException(HttpStatusCode.Unauthorized)
this is my query handler:
namespace Application.User
{
public class Login
{
public class Query : IRequest<AppUser>
{
public string Email { get; set; }
public string Password { get; set; }
}
public class QueryValidator : AbstractValidator<Query>
{
public QueryValidator()
{
RuleFor(x => x.Email).NotEmpty();
RuleFor(x => x.Password).NotEmpty();
}
}
public class Handler : IRequestHandler<Query, AppUser>
{
private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signUserManager;
public Handler(UserManager<AppUser> userManager, SignInManager<AppUser> signInManager)
{
_userManager = userManager;
_signUserManager = signInManager;
}
public async Task<AppUser> Handle(Query request, CancellationToken cancellationToken)
{
var user = await _userManager.FindByEmailAsync(request.Email);
if (user == null)
throw new RestException(HttpStatusCode.Unauthorized);
var result = await _signUserManager.CheckPasswordSignInAsync(user, request.Password, false);
if (result.Succeeded)
{
//TODO : generate token
return user;
}
throw new RestException(HttpStatusCode.Unauthorized);
}
}
}
}

Cannot Resolve Constructor 'Beach(java.lang.string, java.lang.string)'

I implemented an upload image in a fragment. I have a model named Beach.java. However, I'm encountering an error
Cannot resolve constructor 'Beach(java.lang.string,
java.lang.string)' on newBeach = new
Beach(beach_name.getText().toString(), uri.toString());
Here is the code in HomeFragment:
imageFolder.putFile(saveUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mDialog.dismiss();
Toast.makeText(getActivity(), "Uploaded", Toast.LENGTH_SHORT).show();
imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
newBeach = new Beach(beach_name.getText().toString(), uri.toString());
}
});
}
})
Here is the code in Beach.class:
public class Beach {
private String name, image, description, price, menuID;
public Beach() {
}
public Beach(String name, String image, String description, String price, String menuID) {
this.name = name;
this.image = image;
this.description = description;
this.price = price;
this.menuID = menuID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getMenuID() {
return menuID;
}
public void setMenuID(String menuID) {
this.menuID = menuID;
}
}
Your Beach class defines 2 constructors, one with no parameters :
public Beach()
and one with 5 Strings :
public Beach(String name, String image, String description, String price, String menuID)
And you try to call a constructor with 2 Strings, which is no where to be found :
new Beach(beach_name.getText().toString(), uri.toString());
So you should add the below constructor to you Beach class :
public Beach(String name, String image) {
this.name = name;
this.image = image;
}
You don't have constructor with 2 parameters of String.
You can initialize class Beach with :
new Beach(name, image, description, price, menuID)
or with
new Beach()
If you want create with only 2 string add another constructor:
public Beach(String name, String image){....}

ControlsFX: AutoCompletion bind to a model class

I have a model class:
public class Client {
private long id;
private String name;
#Override
public String toString() {
return this.name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and I have a Textfield that I want to use auto complete using a list of Client class, using ControlsFX, here is what I did:
AutoCompletionBinding<Client> binding = TextFields.bindAutoCompletion(searchField, SuggestionProvider.create(appState.clients));
Where appStat.Clients is a list of Client class. For some reason I get no suggestions appearing. What am I doing wrong?

Add Property to ControlsFX PropertySheet

I am creating a PropertySheet and I want to add a Property to the sheet. The problem is, what happens if the value of the property changes, the PropertySheet needs to update to reflect those changes. How would I do this?
import java.util.Map;
import javafx.beans.property.Property;
import org.controlsfx.control.PropertySheet;
public class PropertyItem implements PropertySheet.Item {
private Map<String, Property> map;
private String key;
private String name;
private String description;
PropertyItem(Map<String, Property> map, String key, String name, String description){
this.map = map;
this.key = key;
this.name = name;
this.description = description;
}
#Override
public String getCategory() {
return null;
}
#Override
public String getDescription() {
return description;
}
#Override
public String getName() {
return name;
}
#Override
public Class<?> getType() {
return map.get(key).getValue().getClass();
}
#Override
public Object getValue() {
return map.get(key).getValue();
}
#Override
public void setValue(Object arg0) {
map.get(key).setValue(arg0);
}
}
This is probably too late but anyway...
The property editor uses the following method from PropetySheet.Item interface to listen to value changes.
optional<ObservableValue<? extends Object>> getObservableValue();
You need to implement this method on your PropertyItem class.

Why object is not initializing in spring mvc?

Example: User.java
#Component
public class User{
public String name;
public String email;
public User() {
super();
}
public User(String name, String email) {
super();
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
}
HomeController.java
#Controller
public class HomeController {
#Resource
User user;
#RequestMapping(value="/getUsers", method= RequestMethod.POST)
public #ResponseBody User getUser(User user){
return user;
}
}
But user.getName() and user.getEmail() is null. User class is not initializing. Why? Even though I am sending user object at client side as
POST /spring/getUsers HTTP/1.1 Host: localhost:2015 Cache-Control: no-cache {"name":"vinod", "email":"vinod#gmaol.com" }
Here is a sample code. Please go through the documentation once to understand how requestBody annotation works etc.
#Controller
public class HomeController {
#RequestMapping(value="/getUsers", method= RequestMethod.POST)
public #ResponseBody User getUser(#RequestBody User user){
return user;
}
}

Resources