Почему мой API службы restful возвращает сообщение об ошибке? - PullRequest
0 голосов
/ 04 февраля 2020

Мой код, как показано ниже:

package com.calltree_entries.restful;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.calltree_entries.CallTree;
import com.calltree_entries.Manual;
import com.calltree_entries.util.DbOp;

@Path("/ManualService")
public class ManualService {
    private static final Logger logger = LogManager.getLogger(Class.class.getSimpleName());

@Path("/updateManuals")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response updateManuals (@FormParam("callTreeEntryId") int callTreeEntryId,@FormParam("manuals") Manual[] manuals) throws Exception {
...................
}

Вот исходный код руководства:

package com.calltree_entries;

public class Manual {

    public static final int active=1;
    public static final int inactive=0;

    private int manualId;
    private String manualLocation;
    private String description;
    private String lastUpdateDate;
    public Manual() {

    }
    public int getManualId() {
        return manualId;
    }
    public void setManualId(int manualId) {
        this.manualId = manualId;
    }
    public String getManualLocation() {
        return manualLocation;
    }
    public void setManualLocation(String manualLocation) {
        this.manualLocation = manualLocation;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getLastUpdateDate() {
        return lastUpdateDate;
    }
    public void setLastUpdateDate(String lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }
}

Это мой веб. xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>CallTreeAdmin</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>RestfulServices</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.calltree_entries.restful.CallTreeApplication</param-value>
     </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>RestfulServices</servlet-name>
    <url-pattern>/RestfulServices/*</url-pattern>
  </servlet-mapping>    
</web-app>

Это исходный код CallTreeApplication:

package com.calltree_entries.restful;

import org.glassfish.jersey.server.ResourceConfig;

import com.calltree_entries.Manual;

public class CallTreeApplication extends ResourceConfig { 
    public CallTreeApplication () {
        packages("com.calltree_entries.restful");
        register(Manual.class);
    }
}

Сообщение об ошибке выглядит следующим образом:

Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response 
com.calltree_entries.restful.ManualService.updateManuals(int,com.calltree_entries.Manual[]) throws 
java.lang.Exception at index 1.; 
source='ResourceMethod{httpMethod=POST, consumedTypes=[application/json], producedTypes= 
[application/json], 
suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, 
invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class 
com.calltree_entries.restful.ManualService, 
handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@5155fae1]}, 
definitionMethod=public javax.ws.rs.core.Response 
com.calltree_entries.restful.ManualService.updateManuals(int,com.calltree_entries.Manual[]) throws 
java.lang.Exception, parameters=[Parameter [type=int, source=callTreeEntryId, defaultValue=null], 
Parameter [type=class [Lcom.calltree_entries.Manual;, source=manuals, defaultValue=null]], 
responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:371)

Я попытался изменить объявление функции, как показано ниже:

@Path("/updateManuals")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response updateManuals (@FormParam("callTreeEntryId") int callTreeEntryId,@FormParam("manuals") List<Manual> manuals) throws Exception { 

Однако результат тот же.

На самом деле, я не отправляю файл в службу restful. Я просто хочу отправить 1 текстовое поле и объект массива в службу отдыха.

  1. Этот API не вызывается во время запуска приложения tomcat. Скажите, почему всплывающее сообщение об ошибке при запуске приложения tomcat? Однако это

  2. Когда я удаляю параметр «@FormParam (« manual ») Manual [] manual», сообщение об ошибке исчезло, не могли бы вы сказать мне почему?

...