如何在spring hibernate中发布json对象

时间:2015-03-15 09:05:56

标签: java json spring hibernate

我的pojo是

@Entity
@Table(name="userdetails")
public class UserDetails {
    @Id
    @Column(name="username")
    private String username;

    @Column(name="firstname")
    private String firstname;

    @Column(name="lastname")
    private String lastname;

    @Column(name="password")
    private String password;}//getter and setter

用于存储json对象的RegisterInput类

@JsonSerialize
public class RegisterInput implements Serializable{

    private static final long serialVersionUID = 1L;

    private String username;

    private String firstname;

    private String lastname;

    private String password;//getter and setters

我的服务类

@Transactional public void addUserDetails(UserDetails userDetails) {
        userDetailsDao.addUserDetails(userDetails);
        }

我的Dao课

@Autowired
    private SessionFactory sessionFactory;

    public void addUserDetails(UserDetails userDetails) {
        sessionFactory.getCurrentSession().save(userDetails);

    }

我的控制器类

@RestController
@RequestMapping("/user")
public class UserDetailsController {

@Autowired
private UserDetailsService userDetailsService;

@RequestMapping(value="/details", method= RequestMethod.PUT ,
                                  consumes= MediaType.APPLICATION_JSON_VALUE ,
                                  produces= MediaType.APPLICATION_JSON_VALUE)
@ResponseBody public UserDetails addUserDetails(@RequestBody 
                            RegisterInput registerInput)throws ValidationException{

    if(registerInput==null || registerInput.getUsername()==null){
        throw new ValidationException("Input is not valid");
    }
    UserDetails userDetails=new UserDetails(registerInput.getUsername(),
            registerInput.getFirstname(), registerInput.getLastname(), registerInput.getPassword());
    userDetailsService.addUserDetails(userDetails);
    return userDetails;
}

我的pom是

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
         <!-- Jackson   -->
<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${org.springframework.version}</version>
   </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.10.Final</version>
        </dependency>

我需要动态使用json对象,以便将其存储到我的数据库表中。任何帮助都会被贬低。但每当我将json对象传递给资源映射时,它都会显示错误415,媒体不受支持。

0 个答案:

没有答案