0
votes

I have a bean which extends from org.springframework.security.core.userdetails.User and using this bean with openID. The bean looks like:

package com.employee;

import java.util.Collection;

import javax.xml.bind.annotation.XmlRootElement;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;

@XmlRootElement(name="Employee")
public class Employee extends User{

    private int empId;
    private String deptName;

    public Employee() {
        super("", "unused", null);
    }
     public Employee(String username, Collection<GrantedAuthority> authorities) {
           super(username, "unused", authorities);
       }

    public int getEmpId() {
        return empId;
    }
    public void setEmpId(int empId) {
        this.empId = empId;
    }
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
}

The controller accepts an instance of Employee as:

@RequestMapping(value = "/addEmp.do", method = RequestMethod.POST) public @ResponseBody String addEmployee(@RequestBody Employee emp) {...}

I have sent a POST request with following body:

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
    <empId>10</empId>
    <deptName>abc</deptName>
</Employee>

But getting the following JAXB exception on invoking the addEmp operation:

2012-12-16 15:25:55,364{HH:mm:ss} DEBUG [http-bio-8080-exec-1] (AbstractHandlerExceptionResolver.java:132) -
Resolving exception from handler [public java.lang.String com.main.EmpController.addEmployee(com .employee.Employee)]: 
org.springframework.http.converter.HttpMessageNotReadableException: Could not unmarshal to 
[class com.employee.Employee]: Unable to create an instance of com.employee.Employee; nested exception is 
javax.xml.bind.UnmarshalException: Unable to create an instance of com.employee.Employee
1
Are you sure there's nothing more in the logs from JAXB? Have you tried invoking JAXB manually with this input (in some playground project)? - Marcel Stör

1 Answers

1
votes

The error message:

Unable to create an instance of com.employee

may imply that the input does not provide valid employee properties. In the constructor

public Employee() {
    super("", "unused", null);
}

the default username is set to '', but spring security does not accept blank username.