#@RepositoryRestResource except the @Id

1 messages · Page 1 of 1 (latest)

nova forge
#

The Repository rest resouce gives me links for each resource like for id 1 it gives me their whole record thats in the h2 db and if i type in the id as resource like aliens/{any_id} then it gives me that record. Over here, I want it so if i type aliens/{any_name} it should give me a record with that name on it. Is it possible? i've made a workaround by using built in method in interface where a list of that record is given and i can use it from the controller and return it as data in the form of list

Controller code

package com.example.demo.model.RestController;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.AlienRepo;
import com.example.demo.model.Alien;

@RestController
public class RestControl {

    @Autowired
    AlienRepo ar;

    @GetMapping(path="/aliens/{aname}")
    public List<Alien> getByName(@PathVariable("aname") String name){
        return ar.findByAname(name);
    }
}

Interface or Repository code

package com.example.demo;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.example.demo.model.Alien;
@RepositoryRestResource(collectionResourceRel="aliens",path="aliens")
public interface AlienRepo extends JpaRepository<Alien, Integer>{
    List<Alien> findByAname(String aname);
}

my model contains the aid set with @Id, aname as String and tech as String

hasty vesselBOT
#

<@&987246584574140416> please have a look, thanks.

nova forge
#

🤨

sage moon
#

If name is unique in the data-model, then you don't need the list. You could say something like Optional<String> findOneByName(String name)

hasty vesselBOT
#

@nova forge

Your question has been closed due to inactivity.

If it was not resolved yet, click the button below to keep it
open, or feel free to create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

nova forge
#

Or in a controller

#

Wait what I want is the code to recognize something like /aliens/name
If it can already recognize the resource /aliens/any id

#

What you said is what I somewhat did do