jersey @PathParam: วิธีส่งตัวแปรที่มีเครื่องหมายทับมากกว่าหนึ่งอัน

package com.java4s;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/customers")
public class RestServicePathParamJava4s {
    @GET
    @Path("{name}/{country}")
    @Produces("text/html")
    public Response getResultByPassingValue(
                @PathParam("name") String name,
                @PathParam("country") String country) {

        String output = "Customer name - "+name+", Country - "+country+"";
        return Response.status(200).entity(output).build();

    }
}

ใน web.xml ฉันได้ระบุรูปแบบ URL เป็น /rest/* และใน RestServicePathParamJava4s.java เราระบุระดับคลาส @Path เป็น /customers และระดับเมธอด @Path เป็น {name}/{country}

ดังนั้น URL สุดท้ายควรเป็น

http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4/USA

และควรแสดงการตอบสนองเป็น

Customer name - Java4, Country - USA

ถ้าฉันให้อินพุต 2 ที่ระบุด้านล่างแสดงว่ามีข้อผิดพลาด วิธีแก้ปัญหานี้?

http://localhost:2013/RestPathParamAnnotationExample/rest/customers/Java4:kum77./@.com/USA` 

ที่นี่ Java4:kum77./@.com อันนี้เป็นหนึ่งสตริงและมีเครื่องหมายทับ วิธียอมรับสิ่งนี้โดยใช้ @PathParam หรืออะไรก็ตามที่ฉันต้องใช้ MultivaluedMap หากใครรู้สิ่งนี้โปรดช่วยฉันด้วย ถ้าใครรู้ว่า MultivaluedMap คืออะไร ขอยกตัวอย่างง่ายๆ ให้ฉันหน่อย


person Vinay Kumar    schedule 06.04.2015    source แหล่งที่มา
comment
ตามที่ @peeskillet ตอบไปแล้ว สามารถใช้นิพจน์ทั่วไปได้ สิ่งนี้ยังได้กล่าวถึงในข้อกำหนด JAX-RS อีกด้วย (ส่วนที่ 3.4 เทมเพลต URI)   -  person Abhishek    schedule 07.04.2015


คำตอบ (1)


คุณจะต้องใช้ regex สำหรับสำหรับนิพจน์เส้นทาง name

@Path("{name: .*}/{country}")

ซึ่งจะทำให้ทุกอย่างอยู่ในเทมเพลต name และส่วนสุดท้ายจะเป็น country

ทดสอบ

@Path("path")
public class PathParamResource {

    @GET
    @Path("{name: .*}/{country}")
    @Produces("text/html")
    public Response getResultByPassingValue(@PathParam("name") String name,
                                            @PathParam("country") String country) {

        String output = "Customer name - " + name + ", Country - " + country + "";
        return Response.status(200).entity(output).build();
    }
}

$ curl http://localhost:8080/api/path/Java4:kum77./@.com/USA
Customer name - Java4:kum77./@.com, Country - USA

$ curl http://localhost:8080/api/path/Java4/USA
Customer name - Java4, Country - USA

person Paul Samsotha    schedule 06.04.2015
comment
@ paul-samsotha .* แตกต่างจาก .+ อย่างไรตามที่กล่าวไว้ใน คำตอบนี้ - person bytesandcaffeine; 30.11.2018
comment
@Shamil ในภาษาส่วนใหญ่ นี่คือคำตอบ มันเกี่ยวข้องกับ regex - person Paul Samsotha; 01.12.2018