How to return a singleton list in an API?

collections.singletonlist multiple elements
java singleton arraylist
singleton list python
java add element to singleton list
arrays singleton list
singletonlist c#
java singletonlist vs immutablelist
java arrays singleton list

I am in the middle of learning to create a Java API using Eclipse IDE and Spring-Boot framework. Therefore, I am facing a syntax issue which I am not able to resolve. Below is my code for your reference:

package first.microservice.moviecatalogservice.resources;

import java.util.Collections;
import java.util.List;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import first.microservice.moviecatalogservice.models.CatalogItem;

@RestController 
@RequestMapping("/catalog")
public class MovieCatalogResource {
        
    @RequestMapping("/{user_id}")   
    public List<CatalogItem> getCatalog(@PathVariable("user_id") String user_id)
        {
            return Collections.singletonList(
                     <CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4)
                    );
        }
}

Another code which has the CatalogItem class:

package first.microservice.moviecatalogservice.models;

public class CatalogItem {

    private String Name;
    private String Desc;
    private int Rating;
    
    public CatalogItem(String name, String desc, int rating) {
        Name = name;
        Desc = desc;
        Rating = rating;
    }
    
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }
    public String getDesc() {
        return Desc;
    }
    public void setDesc(String desc) {
        Desc = desc;
    }
    public int getRating() {
        return Rating;
    }
    public void setRating(int rating) {
        Rating = rating;
    }
    
    
}

I wish to enter the URL pattern to display the hardcoded values of the catalogitem to display on the browser.

I am facing error in the following line:

return Collections.singletonList(
                     <CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4)
                    );

The error states that:

The method singletonList(T) in the type Collections is not applicable for the arguments (CatalogItem)

Multiple markers at this line
    - Syntax error on token "<", invalid Expression
    - Syntax error on token ":", invalid     AssignmentOperator
    - name cannot be resolved to a variable
    - Syntax error on token ":", invalid     AssignmentOperator
    - desc cannot be resolved to a variable
    - Syntax error on token ":", invalid     AssignmentOperator
    - rating cannot be resolved to a variable

AFAIK Java doesn't support named parameters. Therefore, this line

<CatalogItem> new CatalogItem(name: "DonJon", desc: "Test", rating: 4)

will give the syntax error you're facing. Change it for

new CatalogItem("DonJon", "Test", 4)

And it should work

Collections singletonList() method in Java with Examples , The singletonList() method of java.util.Collections class is used to return an immutable list containing only the specified object. The returned list� java - How to return a singleton list in an API? 2019-11-13 java 2 Comments 32 I am in middle of learning to create a Java API using Eclipse IDE and Spring-Boot framework.

Singleton List Showdown: Collections::singletonList Vs. List::of , A wonderful new API addition was included in Java 9 called List::of that accepts one or more arguments and returns a List of those arguments. The returned list is serializable. Syntax: public static List singletonList(T o) Parameters: This method takes the object o as a parameter to be stored in the returned list. Return Value: This method returns an immutable list containing only the specified object. Below are the examples to illustrate the singletonList() method

I hope there is an compile time error in your getCatalog method. Please change the return statement like below:

public List<CatalogItem> getCatalog(@PathVariable("user_id") String user_id)
        {
            return Collections.singletonList(new CatalogItem("DonJon", "Test", 4));
        }

java.util.Collections#singletonList, You may want to check out the right sidebar which shows the related API usage. singletonList("ALTER INDEX")); } @Override public MutationState execute() throws @Bean public Catalog catalog() { return new Catalog(Collections. To create an OData V4 endpoint in Web API, see Create an OData v4 Endpoint Using ASP.NET Web API 2.2. We'll create a singleton in your Web API project using the following data model: A singleton named Umbrella will be defined based on type Company, and an entity set named Employees will be defined based on type Employee. The solution used in

SingletonList (Lists 1.2.0 API), Replaces the element at the specified position in this list with the specified element (optional operation). int, size(). Returns the number of elements in this� Some examples I’ve seen or written myself include using a singleton as the source of config settings for a web app, on the client side for anything initiated with an API key (you usually don’t

Java Collections - Collections.singletonList() Examples, Object java.util.Collections LogicBig. Method: public static <T> List<T> singletonList(T o). Returns an immutable list containing only the specified object. New to Web API, so, excuse me if this is a dumb question :) In WCF you can mark a service as a singleton (a threaded singleton) using attributes like so: [ServiceBehavior( IncludeExceptionDetailInFaults = false, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple )]

Collections.SingletonList(Object) Method (Java.Util), [Android.Runtime.Register("singletonList", "(Ljava/lang/Object;)Ljava/util/List;", "")] [Java.Interop.JavaTypeParameters(new System.String[] { "T" })] public static� An application of this method is to remove an element from Collections like List and Set. Syntax: public static Set singleton(T obj) and public static List singletonList(T obj) Parameters: obj : the sole object to be stored in the returned list or set. Return: an immutable list/set containing only the specified object.

Comments
  • What is the exact issue are you facing? Are you getting some compile time error. Please share your log/Error to understand it better.
  • what is the URL u are calling from browser ?
  • Yup you got it, he is facing the same issue :)
  • Multiple markers at this line - Syntax error on token "<", invalid Expression - Syntax error on token "<", delete this token - CatalogItem cannot be resolved to a variable
  • Having issues stated above ^ @Pelocho
  • Yep, just noticed. Thanks.
  • Multiple markers at this line - Syntax error on token "<", invalid Expression - Syntax error on token "<", delete this token - CatalogItem cannot be resolved to a variable
  • I am getting a red line under the CatalogItem name, and getting the following error as stated above.
  • Yep, just noticed. Thanks.