简体   繁体   English

@Path注释要求?

[英]@Path Annotation requirements?

I'm new to specifying resource routes in Java, and I'm having issues specifying the route. 我是Java中指定资源路由的新手,但是在指定路由时遇到了问题。 So far I have one Class which simply extends Application , and one class that reacts to input. 到目前为止,我有一个仅扩展Application类,以及一个对输入做出反应的类。 What are the routing requirements for these classes? 这些类的路由要求是什么? My code below does not work and im trying to figure out why. 我的以下代码无法正常工作,我试图找出原因。 I've tried to find sources for these, but haven't had much luck. 我已经尝试找到这些资源的来源,但是运气并不好。

  1. Can I just use a / for the ApplicationPath? 我可以仅将/用作ApplicationPath吗? All this class does is extend Application so it can find routes. 该类所做的只是扩展Application以便它可以找到路由。

Example: 例:

package com.sentiment360.helloworld;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
 * JAXActivator is an arbitrary name, what is important is that javax.ws.rs.core.Application is extended
 * and the @ApplicationPath annotation is used with a "rest" path.  Without this the rest routes linked to
 * from index.html would not be found.
 */
@ApplicationPath("/")
public class JAXActivator extends Application {
}

Does each class need to have a declared @Path (or can they all be @Stateless)? 每个类是否都需要有一个声明的@Path(或者它们都可以是@Stateless)?

@Path("/helloservice")
public class HelloService {

    private static Logger _logger;

    public HelloService(){
        _logger = Logger.getLogger(HelloService.class.getName());
    }

    private Connection conn() throws SQLException  {...}
}

The short version for #1 is yes. #1的简短版本是。

BUT: behavior is implementation dependent. 但是:行为取决于实现。 See https://stackoverflow.com/a/16747253/1063501 for a thorough explanation. 有关详细说明,请参见https://stackoverflow.com/a/16747253/1063501

As for #2, yes, you generally need to specify a @Path for every endpoint you want. 对于#2,是的,通常需要为每个所需的端点指定一个@Path The fact that it is @Stateless is irrelevant as you'll need a way to address it. 它是@Stateless的事实是无关紧要的,因为您需要一种解决它的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM