0

私はJava、Maven、Hibernate 3 / JPA、Eclipseを使用して、Mysqlデータベースにデータを取り込むためのPUTメソッドを実装しています。

これが私のPOJOです

 import static javax.persistence.GenerationType.IDENTITY;
    import java.util.Date;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;
    import javax.persistence.UniqueConstraint;

    @Entity
    @Table(name = "Person", catalog = "mydb", uniqueConstraints = {
                        @UniqueConstraint(columnNames = "Person"),})
    public class Person implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    private Integer id;
    private String Name;
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
    return id;
    }

    public void setId(Integer id) {
    this.id = id;
    }

    @Column(name = "name", unique = true, nullable = false, length = 30)
    public String getName() {
    return flowName;
    }

    public void setName(String Name) {
    this.Name = Name;
    }

    }

これが私のアノテーションクラスです。

import javax.ws.rs.Consumes;
    import javax.ws.rs.PUT;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    import org.apache.log4j.Logger;
    import org.hibernate.Session;
    import com.google.gson.Gson;
    import com.tracker.domain.Flow;
    import com.tracker.persistence.HibernateUtil;

    public class PersonService {
    private Logger LOG = Logger.getLogger(TrackerService.class);

    String JsonString = "{\"name\":\"John Doe\"}";
    Gson gson = new Gson();
    Person person = gson.fromJson(JsonString,Person.class);

    @PUT
    @Path("")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void processandSaveJson(Person person) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    String Name = Person.getName();
    person.setName(Name);
    session.beginTransaction();
    session.save(person);
    session.getTransaction().commit();
    }
    }

これが私のHibernate.Utilです。

import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;

    public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();
    private static SessionFactory buildSessionFactory() {

    try {
    // Create the SessionFactory from hibernate.cfg.xml
    return new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {

    // Make sure you log the exception, as it might be swallowed
    System.err.println("Initial SessionFactory creation failed." + ex);
    throw new ExceptionInInitializerError(ex);
    }
    }



 public static SessionFactory getSessionFactory() {
    return sessionFactory;
    }

    public static void shutdown() {
    // Close caches and connection pools
    getSessionFactory().close();
    }
    }

これが私のSessionFactoryContextListenerクラスです

import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    import org.apache.log4j.Logger;
    import org.hibernate.Session;

    @WebListener
    public class SessionFactoryListener implements ServletContextListener {
    private Logger LOG = Logger.getLogger(SessionFactoryListener.class);

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
    if (LOG.isInfoEnabled()) {
    LOG.info("\n\tInside contextInitialized()---\n");
    }

    Session session = HibernateUtil.getSessionFactory().openSession();
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
    if (LOG.isInfoEnabled()) {
    LOG.info("\n\tInside contextDestroyed()\n");
    }

    HibernateUtil.shutdown();   
    }   

    }

Tomcatサーバーを使用してこれを実行しようとすると、次のエラーが発生します。

type Status report
message Method Not Allowed
description The specified HTTP method is not allowed for the requested resource.

私はこれに非常に新しいです。私が間違っていることを教えてください。上記の値を使用してmysqlデータベースにレコードを挿入しようとしています。親切に私を助けてください。

ありがとう、ジャック

4

1 に答える 1

0

コメントに記載されているように、呼び出しコードを残りと一緒に提供する必要があります。ただし、ブラウザを使用してリクエストを行っていると既に述べたので、javadcript を使用せずに「put」をサポートするブラウザはほとんどまたはまったくないことに注意してください。あなたがしていることは、単純な「get」のように見えます。したがって、解決策は、フォーム送信で JavaScript を使用するか、REST を破棄してメソッドを反映する URL を用意することです (例: /person/new/ および /person/{personId})。

于 2012-12-23T11:06:31.097 に答える