gpt4 book ai didi

java - 难以在我的 Rest 服务中实现 PUT 注释

转载 作者:行者123 更新时间:2023-11-28 23:46:16 25 4
gpt4 key购买 nike

我正在使用 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();
}
}

这是我的 SessionFactory 上下文监听器类

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 数据库中。请帮助我。

谢谢, jack

最佳答案

如评论中所述,您应该连同其余部分一起提供您的调用代码。但是由于您已经提到您正在使用浏览器发出请求,所以应该提到大多数/没有浏览器支持不使用 javadcript 的“put”。你在做什么看起来像一个简单的“得到”。所以解决方案是要么在表单提交中使用 javascript,要么放弃 REST 并使用反射(reflect)该方法的 Urls(例如/person/new/和/person/{personId}

关于java - 难以在我的 Rest 服务中实现 PUT 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14007853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com