gpt4 book ai didi

java - Jersey 2 + Spring 4 + jetty-maven-plugin

转载 作者:行者123 更新时间:2023-12-01 15:19:02 24 4
gpt4 key购买 nike

我想举一个使用 jersey 2 + spring 4 + jetty-maven-plugin 的例子。但是一直收到这个错误,不明白为什么..请帮帮我。

WARNING: The Jersey servlet application, named com.joejag.code.orders.restservices.ResourceConfiguration, is not annotated with ApplicationPath and has no servlet mapping.
2015-12-16 19:56:38.746:INFO:/.0-SNAPSHOT:main: Spring WebApplicationInitializers detected on classpath: [org.glassfish.jersey.server.spring.SpringWebApplicationInitializer@2776015d]
2015-12-16 19:56:38.778:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.m.p.JettyWebAppContext@15fb7a32{/orders-server-1.0-SNAPSHOT,file:///home/bryan-1/workspace/project/java/simple-java-restful-service-using-jersey-and-maven-master/src/main/webapp/,STARTING}{file:///home/bryan-1/workspace/project/java/simple-java-restful-service-using-jersey-and-maven-master/src/main/webapp/}
java.lang.IllegalStateException: No such servlet: Example

我的 POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.joejag.code.orders</groupId>
<artifactId>orders-server</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.3.RELEASE</spring.version>
<jersey.version>2.22.1</jersey.version>
</properties>

<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>https://maven.java.net/content/groups/public/</url>
<layout>default</layout>
</repository>
</repositories>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>

<!-- Jersey dependencies -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>

<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Jersey + Spring -->
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring4</artifactId>
<version>3.0-SNAPSHOT</version>
</dependency>
</dependencies>

<build>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>

<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.6.v20151106</version>
<configuration>
<scanTargets>
<scanTarget>${project.basedir}/src/main</scanTarget>
<scanTarget>${project.basedir}/src/test</scanTarget>
</scanTargets>
<webAppConfig>
<contextPath>/${project.artifactId}-${project.version}</contextPath>
</webAppConfig>
<contextPath>${project.artifactId}</contextPath>
</configuration>
</plugin>
</plugins>
</build>

</project>

我的 web.xml

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Example</display-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.joejag.code.orders.restservices.ResourceConfiguration</param-value>
</init-param>
<!-- <init-param> <param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.joejag.code.orders.restservices</param-value> </init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>Example</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

我的 ResourceConfig 类

package com.joejag.code.orders.restservices;

import org.glassfish.jersey.server.ResourceConfig;

public class ResourceConfiguration extends ResourceConfig {
public ResourceConfiguration() {
register(OrdersService.class);
}

}

我的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.joejag.code.orders.restservices" />

<bean id="transactionBo" class="TransactionBoImpl" />
<!-- <bean class="OrderService" /> -->
</beans>

已编辑

在修复不一致的servlet-name 问题后,Jetty 能够加载servlet,但是出现了一个新问题,即所有@PUT 方法似乎不再执行。这是 OrderService 的样子。

package com.joejag.code.orders.restservices;

import java.util.Map;
import java.util.TreeMap;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
// * curl -X PUT http://127.0.0.1:8080/orders-server/orders/1?customer_name=bob
// * curl -X GET http://127.0.0.1:8080/orders-server/orders/1
* curl -X GET http://127.0.0.1:8080/orders-server/orders/list
*/

@Component
@Path("orders")
public class OrdersService
{
public static Map<String, String> orders = new TreeMap<String, String>();

//@Autowired
//private TransactionBo transactionBo;


public OrdersService()
{
//his.transactionBo = transactionBo;
}

@Path("/{order}")
@PUT
@Produces("text/html")
public String create(@PathParam("order") String order, @QueryParam("customer_name") String customerName)
{
orders.put(order, customerName);

return "Added order #" + order ;//+ this.transaction.save();
}

@Path("/{order}")
@GET
@Produces("text/html")
public String find(@PathParam("order") String order)
{
if (orders.containsKey(order))
return "<h2>Details on Order #" + order + "</h2><p>Customer name: " + orders.get(order);

throw new WebApplicationException(Response.Status.NOT_FOUND);
}

@Path("/list")
@GET
@Produces("text/html")
public String list()
{
String header = "<h2>All Orders</h2>\n";

header += "<ul>";
for (Map.Entry<String, String> order : orders.entrySet())
header += "\n<li>#" + order.getKey() + " for " + order.getValue() + "</li>";

header += "\n</ul>";

return header;
}
}

最佳答案

web.xml 中的

servlet-name 必须保持一致。修复后, jetty 就可以加载 servlet。

关于java - Jersey 2 + Spring 4 + jetty-maven-plugin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326436/

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