1

我有一个从 org.springframework.security.core.userdetails.User 扩展的 bean,并将这个 bean 与 openID 一起使用。豆看起来像:

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;
    }
}

控制器接受 Employee 的实例:

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

我已经发送了一个带有以下正文的 POST 请求:

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

但是在调用 addEmp 操作时出现以下 JAXB 异常:

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
4

1 回答 1

1

错误信息:

无法创建 com.employee 的实例

可能暗示输入未提供有效的员工属性。在构造函数中

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

默认用户名设置为'',但spring security不接受空白用户名。

于 2012-12-16T16:07:55.800 回答