I have a following jersey class .
#Path("/static1/static2")
public class DoStuff {
#POST
#Path("/static3")
#Consumes(MediaType.APPLICATION_XML)
#Produces("application/xml")
public Response validation(String inputXML){
so my url is localhost/static1/static2/static3 and I get a 200
my goal is to have a URL that is
localhost/static1/{variable}/static2/static3
I tried modifying my class in the following way
#Path("/static1/{variable}/static2")
public class DoStuff {
#POST
#Path("/static3")
#Consumes(MediaType.APPLICATION_XML)
#Produces("application/xml")
public Response validation(String inputXML){
but I keep getting a 404 , what am I doing wrong ?
The problem seems to be with the last path segment static3.{format}. Try the following:
#Path("/static1/{variable}/static2")
public class DoStuff {
#POST
#Path("/{segment3:static3.*}")
#Consumes(MediaType.APPLICATION_XML)
#Produces("application/xml")
public Response validation(#PathParam("variable") String variable,
#PathParam("segment3") String segment3,
String inputXML) {
...............
}
Related
is there any way to set general header on response of all api paths in JaxRS ?
for example i have a api like this :
#Path("/api/v1")
public class JaxRsConfig extends Application {
}
and
#Path("/voucher")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class Voucher {
#POST
public Response add(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
#GET
public Response get(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
#GET
public Response list(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
}
and this:
#Path("/invoice")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class Invoice {
#POST
public Response add(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
#GET
public Response get(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
#GET
public Response list(...) {
return Response.ok().header("API_EXPIRE_DATE","2025/05/12").build();
}
}
I always have to put this header in the response .
JaxRs has any mechanism to set this header generally ?
Note: I use JavaEE-8 on Liberty Application Server
You can try to use a WriterInterceptor to add this header.
Some good examples explained here: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/content/en/part1/chapter12/reader_and_writer_interceptors.html
Scenario-1 : During my work I encountered below scenario, On which : getText1, getText2,getText3,getText4,getText5,getText6 are without #Path annotations,
But when I call the API (http://localhost:8080/.../testqa/ )it always returns following result :
{
"name" : "Sumit1 Arora",
"age" : 21,
"address" : "Lakshay1 Arora"
}
SimpleQAImpl
#Service("qaservice")
#Path("/testqa")
public class SimpleQAImpl {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/simpleqa")
public Person getText() {
return new Person("Sumit Arora",21,"Lakshay Arora");
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Person getText1() {
return new Person("Sumit1 Arora",21,"Lakshay1 Arora");
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Person getText3() {
return new Person("Sumit3 Arora",21,"Lakshay3 Arora");
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Person getText4() {
return new Person("Sumit4 Arora",21,"Lakshay4 Arora");
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Person getText5() {
return new Person("Sumit5 Arora",21,"Lakshay5 Arora");
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Person getText6() {
return new Person("Sumit6 Arora",21,"Lakshay6 Arora");
}
}
May you please tell me how Apache CXF works, if #Path not given like the case above or on other scenarios as well?
Is there any reference to understand such stuff?
Scenario-2 : On this scenario, No #Path variable defined on top of API Call, how all of these API would be called from URI ?
#Service
#Path("/customer")
public class CustomerResource {
private final Logger logger = LoggerFactory.getLogger(CustomerResource.class);
#Autowired
private CustomerService customerService;
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response create(Customer customer) {
if(customerService.createCustomer(customer).isPresent()) {
return Response.ok().build();
} else
return Response.status(Response.Status.BAD_REQUEST).entity(new Error(1,"test")).build();
}
#GET
#Produces(MediaType.APPLICATION_JSON)
public Response getAll() {
logger.debug("Received request to fetch all the customers.");
List<Customer> customers = customerService.fetchAll();
GenericEntity<List<Customer>> customerEntities = new GenericEntity<List<Customer>>(customers) {};
return Response.ok(customerEntities).build();
}
#PUT
#Consumes(MediaType.APPLICATION_JSON)
public Response update(Customer customer) {
return Response.status(Response.Status.NO_CONTENT).build();
}
}
The documentation for how CXF selects which method is executed is here: CXF resource selection overview. The docs talks about which method it prefers by looking at which has more path parameters or more a more specific path but each method in your first scenario has the same path so the first one is chosen. To differentiate between them you could use a path parameter.
The Second scenario requires you to change the HTTP method used with the URL so:
POST /customer
GET /customer
PUT /customer
would each invoke the different methods.
I have following jersey method declaration:
#POST
#Path("/fooPath")
#Produces({MediaType.APPLICATION_JSON})
#Consumes({MediaType.APPLICATION_JSON})
public Response isSellableOnline(#FormParam("productCodes") final List<String> productCodes,
#FormParam("storeName") final String storeName,
#Context HttpServletRequest request) {
In rest client I try to invoke following method like this:
When I debug method I see that received parameters are null:
How to rewrite method declaration?
It is because on the isSellableOnlie method you are expecting or trying to extract form parameters, but the incoming POST request is JSON.
Well if you want JSON you should make POJO Class to be able to serialize the JSON.
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Store {
private String storeName;
private List<String> productCodes;
public Store() {
}
public String getName() {
return name;
}
public List<String> getProductCodes() {
return productCodes;
}
}
And then in your method:
#POST
#Path("/fooPath")
#Produces({MediaType.APPLICATION_JSON})
#Consumes({MediaType.APPLICATION_JSON})
public Response isSellableOnline(Store store) {
store.getName();
...
}
I am using Jersey RESTful web services. I have below resource.
#Path("/banker/{location}")
#Component
public class BankResource{
#GET
#Path("/{id}")
#Produces({MediaType.APPLICATION_XML})
#Override
public Response getBankerByID(#PathParam("id") String id) {
//some logic
}
}
In above class how can i get the value of {location} ? Because based on {location} value i need to do some processing. Is it possible to get its value?
Thanks!
You can get the location value using #PathParam and map it to method parameter as follows.
`
#Path("/banker/{location}")
#Component
public class BankResource{
#GET
#Path("/{id}")
#Produces({MediaType.APPLICATION_XML})
#Override
public Response getBankerByID(#PathParam("location") String location,
#PathParam("id") String id) {
//your code here
}
`
I'm just wondering how to modify the following
#POST
#Consumes(MediaType.APPLICATION_JSON)
public Response createObject(Object object) {
...
}
to also allow a path parameter? I was thinking something like
#POST
#Path("{server}")
#Consumes(MediaType.APPLICATION_JSON)
public Response createObjectOnServer(#PathParam("server") String url, Object object) {
...
}
but that either is just wrong or I don't know how the json should be structured.
The second block of code should work, in my project:
#POST
#Path("/{mode}")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.TEXT_PLAIN)
public String renderWidget(#PathParam("mode") String mode,RenderingRequest renderingRequest){
...
}
where 'mode' is a path param and 'RenderingRequest' is a pojo that maps the request body(a json).