gpt4 book ai didi

java - 如何解决 Jersey 错误 服务器遇到意外情况,无法满足请求

转载 作者:行者123 更新时间:2023-12-01 16:43:42 26 4
gpt4 key购买 nike

我尝试学习 Jersey 和 Make Rest API,我使用 JDK 8 来开发这个 API

index.jsp

<html>
<body>
<h2>Jersey RESTful Web Application!</h2>
<p><a href="webapi/myresource">Jersey resource</a>
<p>Visit <a href="http://jersey.java.net">Project Jersey website</a>
for more information on Jersey!
</body>
</html>

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.example</groupId>
<artifactId>LatihanJerseyCrud</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>LatihanJerseyCrud</name>

<build>
<finalName>LatihanJerseyCrud</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.example.LatihanJerseyCrud</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

型号

类别.java

package com.example.LatihanJerseyCrud.Model;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "kategori")
public class Kategori {
@XmlElement(name = "id_Kategori")
private int id_kat;

@XmlElement(name="nama_kat")
private String nama;

public int getId_kat() {
return id_kat;
}

public void setId_kat(int id_kat) {
this.id_kat = id_kat;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public Kategori(int id_kat, String nama) {
super();
this.id_kat = id_kat;
this.nama = nama;
}

public Kategori() {
super();
// TODO Auto-generated constructor stub
}


}

响应.java

package com.example.LatihanJerseyCrud.Model;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Response {


@XmlElement(name = "message")
private String message;
@XmlElement(name = "status")
private Boolean status;


public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Boolean isStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public Response() {
super();
// TODO Auto-generated constructor stub
}
public Response(String message, Boolean status) {
super();
this.message = message;
this.status = status;
}


}

服务

界面

KategoriService.java

package com.example.LatihanJerseyCrud.Service;

import com.example.LatihanJerseyCrud.Model.*;


public interface KategoriService {
public Response addKategori(Kategori kategori);
public Response deleteKategori(Kategori kategori);
public Response updateKategori(Kategori kategori);
public Kategori[] getAllKategori();

}

实现

KategoriServiceImpl.java

package com.example.LatihanJerseyCrud.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.example.LatihanJerseyCrud.Model.Kategori;
import com.example.LatihanJerseyCrud.Model.Response;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
@Path("/kategori")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public class KategoriServiceImpl implements KategoriService{

private static Map<Integer, Kategori> mapKategori = new HashMap<Integer,Kategori>();

@Override
@POST
@Path("/add")
public Response addKategori(Kategori kategori) {
// TODO Auto-generated method stub
Response response= new Response();
if(mapKategori.get(kategori.getId_kat())!= null){

response.setStatus(false);
response.setMessage("Kategori is Exist!" );
return response;
}

else {
mapKategori.put(kategori.getId_kat(), kategori);
response.setStatus(true);
response.setMessage("Kategori Has been Added Successfully");
return response;
}
}

@Override
@DELETE
@Path("/delete")
public Response deleteKategori(Kategori kategori) {
// TODO Auto-generated method stub
Response response= new Response();
if(mapKategori.get(kategori.getId_kat())==null) {
response.setMessage("Kategori id Is not Exist!");
response.setStatus(false);
return response;
}
else {
response.setMessage("Kategori Has Been Deleted Successfully");
response.setStatus(true);
mapKategori.remove(kategori.getId_kat());
return response;

}

}

@Override
@Path("/update")
public Response updateKategori(Kategori kategori) {
// TODO Auto-generated method stub
Response response =new Response();
if(mapKategori.get(kategori.getId_kat())==null) {
response.setMessage("Kategori id Is not Exist!");
response.setStatus(false);
return response;
}
else
{
mapKategori.put(kategori.getId_kat(), kategori);
response.setStatus(true);
response.setMessage("Kategori Has Been Update Successfully");
return response;
}
}

@Override
@GET
@Path("/getAll")
public Kategori[] getAllKategori() {
// TODO Auto-generated method stub
Set<Integer> ids=mapKategori.keySet();
Kategori[] kategori= new Kategori[ids.size()];
int i=0;
for(Integer id:ids) {
kategori[i]= mapKategori.get(id);
i++;
return kategori;
}
return null;
}
}

尝试点击 PostMant

示例:

方法添加

<强> http://localhost:8080/LatihanJerseyCrud/rest/kategori/add

using Body raw XML

<?xml version="1.0" encoding="UTF-8"?>
<kategori>
<id_Kategori>01</id_Kategori>
<nama_kat>Tepung</nama_kat>
</kategori>

其他方法是相同的响应是

错误

<body>
<h1>HTTP Status 500 – Internal Server Error</h1>
<hr class="line" />
<p><b>Type</b> Exception Report</p>
<p><b>Message</b> Servlet.init() for servlet [Jersey Web Application] threw exception</p>
<p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.
</p>
<p><b>Exception</b></p>
<pre>javax.servlet.ServletException: Servlet.init() for servlet [Jersey Web Application] threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1627)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
</pre>
<p><b>Root Cause</b></p>
<pre>org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] A sub-resource model, public com.example.LatihanJerseyCrud.Model.Response com.example.LatihanJerseyCrud.Service.KategoriServiceImpl.updateKategori(com.example.LatihanJerseyCrud.Model.Kategori), can not have an entity parameter. Try to move the parameter to the corresponding resource method.; source=&#39;ResourceMethod{httpMethod=null, consumedTypes=[], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.example.LatihanJerseyCrud.Service.KategoriServiceImpl, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1cc0b]}, definitionMethod=public com.example.LatihanJerseyCrud.Model.Response com.example.LatihanJerseyCrud.Service.KategoriServiceImpl.updateKategori(com.example.LatihanJerseyCrud.Model.Kategori), parameters=[Parameter [type=class com.example.LatihanJerseyCrud.Model.Kategori, source=null, defaultValue=null]], responseType=class com.example.LatihanJerseyCrud.Model.Response}, nameBindings=[]}&#39;]
org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:371)
org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:293)
org.glassfish.jersey.server.ApplicationHandler$$Lambda$201&#47;31298427.call(Unknown Source)
org.glassfish.jersey.internal.Errors.process(Errors.java:292)
org.glassfish.jersey.internal.Errors.process(Errors.java:274)
org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232)
org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:292)
org.glassfish.jersey.server.ApplicationHandler.&lt;init&gt;(ApplicationHandler.java:259)
org.glassfish.jersey.servlet.WebComponent.&lt;init&gt;(WebComponent.java:311)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:347)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:615)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1627)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
</pre>
<p><b>Note</b> The full stack trace of the root cause is available in the server logs.</p>
<hr class="line" />
<h3>Apache Tomcat/8.5.54</h3>
</body>

我希望有人能帮助我解决这个问题。谢谢!

最佳答案

您忘记在 updateKategori 上指定 HTTP 方法类型。这就是为什么错误显示:

A sub-resource model, public com.example.LatihanJerseyCrud.Model.Response com.example.LatihanJerseyCrud.Service.KategoriServiceImpl.updateKategori(com.example.LatihanJerseyCrud.Model.Kategori), can not have an entity parameter. Try to move the parameter to the corresponding resource method.

关于java - 如何解决 Jersey 错误 服务器遇到意外情况,无法满足请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61814784/

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