我正在从链接中开发Spring boot微服务示例:https://dzone.com/articles/spring-boot-creating.在这个项目中,我只是将父依赖项更新到其最新版本,其他代码文件保持不变。当我单击时,我面临以下错误http://localhost:8080/order?idCustomer=2
2016-09-09 11:46:20.888 ERROR 14152 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by"@Consumes" and "@Produces" annotations at Java methods public java.util.List br.com.alexandreesl.handson.rest.ProductRest.getProducts() and public java.util.List br.com.alexandreesl.handson.rest.CustomerRest.getCustomers() at matching regular expression /. These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@14c14bf']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:555) ~[jersey-server-2.23.1.jar:na]
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184) ~[jersey-server-2.23.1.jar:na]
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350) ~[jersey-server-2.23.1.jar:na]
at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347) ~[jersey-server-2.23.1.jar:na]
at org.glassfish.jersey.internal.Errors.process(Errors.java:315) ~[jersey-common-2.23.1.jar:na]
at org.glassfish.jersey.internal.Errors.process(Errors.java:297) ~[jersey-common-2.23.1.jar:na]
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255) ~[jersey-common-2.23.1.jar:na]
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347) ~[jersey-server-2.23.1.jar:na]
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392) ~[jersey-container-servlet-core-2.23.1.jar:na]
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177) ~[jersey-container-servlet-core-2.23.1.jar:na]
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369) ~[jersey-container-servlet-core-2.23.1.jar:na]
at javax.servlet.GenericServlet.init(GenericServlet.java:158) ~[tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1194) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:133) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:108) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:785) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1425) [tomcat-embed-core-8.5.4.jar:8.5.4]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.4.jar:8.5.4]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_102]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_102]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.4.jar:8.5.4]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_102]
更新的pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
应用.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
ApplicationConfig.java
@Configuration
public class ApplicationConfig {
@Named
static class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
this.packages("br.com.alexandreesl.handson.rest");
}
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}
}
Order.java
public class Order {
private long id;
private long amount;
private Date dateOrder;
private Customer customer;
private Product product;
// setters and getters
}
OrderRest.java
@Named
@Path("/")
public class OrderRest {
private long id = 1;
@Inject
private RestTemplate restTemplate;
@GET
@Path("order")
@Produces(MediaType.APPLICATION_JSON)
public Order submitOrder(@QueryParam("idCustomer") long idCustomer,
@QueryParam("idProduct") long idProduct,
@QueryParam("amount") long amount) {
Customer customer = restTemplate.getForObject("http://localhost:8080/customer?id={id}", Customer.class, idCustomer);
Product product = restTemplate.getForObject("http://localhost:8080/product?id={id}", Product.class,idProduct);
Order order = new Order();
order.setCustomer(customer);
order.setProduct(product);
order.setId(id);
order.setAmount(amount);
order.setDateOrder(new Date());
id++;
return order;
}
}
Customer.java
public class Customer {
private long id;
private String name;
private String email;
// setters and getters
}
客户资源.java
@Named
@Path("/")
public class CustomerRest {
private static List<Customer> customers = new ArrayList<Customer>();
static {
Customer customer1 = new Customer();
customer1.setId(1);
customer1.setNome("Customer 1");
customer1.setEmail("customer1@gmail.com");
Customer customer2 = new Customer();
customer2.setId(2);
customer2.setNome("Customer 2");
customer2.setEmail("Customer2@gmail.com");
Customer customer3 = new Customer();
customer3.setId(3);
customer3.setNome("Customer 3");
customer3.setEmail("Customer3@gmail.com");
Customer customer4 = new Customer();
customer4.setId(4);
customer4.setNome("Customer 4");
customer4.setEmail("Customer4@gmail.com");
Customer customer5 = new Customer();
customer5.setId(5);
customer5.setNome("Customer 5");
customer5.setEmail("Customer5@gmail.com");
customers.add(customer1);
customers.add(customer2);
customers.add(customer3);
customers.add(customer4);
customers.add(customer5);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Customer> getCustomers() {
return customers;
}
@GET
@Path("customer")
@Produces(MediaType.APPLICATION_JSON)
public Customer getCustomer(@QueryParam("id") long id) {
Customer cli = null;
for (Customer c : customers) {
if (c.getId() == id)
cli = c;
}
return cli;
}
}
产品.java
public class Product {
private long id;
private String sku;
private String description;
// setters and getters
}
产品机箱.java
@Named
@Path("/")
public class ProductRest {
private static List<Product> products = new ArrayList<Product>();
static {
Product product1 = new Product();
product1.setId(1);
product1.setSku("abcd1");
product1.setDescription("Product1");
Product product2 = new Product();
product2.setId(2);
product2.setSku("abcd2");
product2.setDescription("Product2");
Product product3 = new Product();
product3.setId(3);
product3.setSku("abcd3");
product3.setDescription("Product3");
Product product4 = new Product();
product4.setId(4);
product4.setSku("abcd4");
product4.setDescription("Product4");
products.add(product1);
products.add(product2);
products.add(product3);
products.add(product4);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Product> getProducts() {
return products;
}
@GET
@Path("product")
@Produces(MediaType.APPLICATION_JSON)
public Product getProduct(@QueryParam("id") long id) {
Product prod = null;
for (Product p : products) {
if (p.getId() == id)
prod = p;
}
return prod;
}
}
请在这个问题上指导。
我能够解决这个问题。您需要在映射请求 URL 中进行一些更改。我想知道块中的代码如何无误地工作。
根据我的理解,一个可能的原因是您对该URL调用有两个或多个适用的映射。CustomerRest.java
和ProductRest.java
都映射到/
。
@Path("/")
和
@Path("/")
泽西Jersey
无法判断实际应该调用什么方法,并给出了此错误。
您需要更正以下文件:OrderRest.java
@Named
@Path("/")
public class OrderRest {
private long id = 1;
@Inject
private RestTemplate restTemplate;
@GET
@Path("order")
@Produces(MediaType.APPLICATION_JSON)
public Order submitOrder(@QueryParam("idCustomer") long idCustomer,
@QueryParam("idProduct") long idProduct,
@QueryParam("amount") long amount) {
Customer customer = restTemplate.getForObject("http://localhost:8080/customer/getCustomer?id={id}", Customer.class, idCustomer);
Product product = restTemplate.getForObject("http://localhost:8080/product/getProduct?id={id}", Product.class,idProduct);
Order order = new Order();
order.setCustomer(customer);
order.setProduct(product);
order.setId(id);
order.setAmount(amount);
order.setOrderDate(new Date());
id++;
return order;
}
}
在CustomerRest中。java-使用此
@GET
@Path("/getCustomer")
@Produces(MediaType.APPLICATION_JSON)
public Customer getCustomer(@QueryParam("id") long id) {
Customer cli = null;
for (Customer c : customers) {
if (c.getId() == id)
cli = c;
}
return cli;
}
在产品指南中 - 使用这个
@GET
@Path("/getProduct")
@Produces(MediaType.APPLICATION_JSON)
public Product getProduct(@QueryParam("id") long id) {
Product prod = null;
for (Product p : products) {
if (p.getId() == id)
prod = p;
}
return prod;
}
我有一个Spring web应用程序。当应用程序上下文启动时,我使用执行一些应用程序initalization,特别是从web服务加载数据。如果这个web服务不能到达,那么我的应用程序就不能做任何有用的事情,保持它运行也没有意义。是否有方法以编程方式关闭应用程序上下文? 我已经试着做了以下几点: 那么在这种情况下关闭应用程序的正确方法是什么呢?
当我使用MYSQL数据库运行我的项目时,它工作正常,但当我尝试使用Oracle数据库运行它时,我收到了以下异常。 org.springframework.beans.factory.BeanCreationException: 创建名为“embeddedServletContainerCustomizerBeanPostProcessor”的 bean 时出错:Bean 初始化失败; 编辑1个异常
我有以下情况。在我的微服务[MA]中,我希望在应用程序启动后或基于某个事件初始化/销毁一些bean。想象一下,还有另一个微服务[MB],它保存关于ContentStores的信息。在MA启动之后,我想向MB请求contentStore条目,基于此,我想根据需要创建这么多bean。可能会有一个事件触发storrecreated/storredeleted,在这种情况下,我需要销毁bean。 我目前不
我正在开发我的第一个 Swing 应用程序,现在提出了一个难题:在静态初始化期间或开始实际执行后执行引导和资源初始化。我是什么意思...我有单例: 因此,方法如下所示 或者,也许我在启动后手动初始化资源,然后运行它。逻辑上正确的方式是什么?
我有一个导入到eclipse 3.4中的maven webapp项目,带有logback(1.0.9)日志记录。有: 主/资源/回溯.xml src/test/resources/logback-test.xml 当我在Tomcat中启动webapp时,由于Logback找到配置文件的顺序,logback-test.xml总是被加载。相反,我希望为应用程序启动加载logback.xml,并将log
背景:我构建了一个Jersey应用程序(即一个WAR),并在服务器集群上部署它,在不同的服务器上配置不同的Spring配置,以启用或禁用服务器的不同部分,例如,某些服务器启用了资源,等等。这在Jersey 1.0中非常容易:我只是说, 在Spring配置中,让Jersey扫描特定包并启用其中的JAX-RS资源提供者。 现在在Jersey 2.0中,Spring不起作用,因此必须以编程方式在从派生的