0

I have an application with urls like site.com/article/1/title.htm

I have @RequestMapping /article/{id}/{title}.htm that server this request and gets the article.

What I am looking achieve is to have a url like site.com/title.htm but cant think of a way to do that using Spring MVC because I need id of the article. any ideas? Thanks in advance

Toseef Zafar
  • 1,601
  • 4
  • 28
  • 46

2 Answers2

1

When you create an article, you need to create the SEO-friendly URL also and persist it, along with the article. Now you need to have a repository method that allows you to retrieve articles by permalink, and a Spring MVC endpoint that calls that repository method.

Using the title may not be a good idea, as the title is usually not URL-friendly, and may eventually be non-unique. But it is a good idea to use the title as the input for the permalink.

Here's a sample permalink algorithm:

This is how the read path could look like:

@Autowired
private ArticleRepository ar;

@RequestMapping(value="/article/{id}/{ignored}") @ResponseBody
public Article getByIdAndIgnorePermalink(@PathVariable String id, @PathVariable String ignored){
    return ar.getById(id);
}

@RequestMapping(value="/article/{title}.html") @ResponseBody
public Article getByPermalink(@PathVariable String permalink){
    return ar.getByPermalink(permalink);
}
Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Thanks, can you please elaborate a little more, I never used repository methods before, will still have controller methods with @RequestMapping? if yes, then what the value will look like? – Toseef Zafar Aug 29 '16 at 11:57
  • well where does the current code get the article from? usually that would be a article repository or article service. whichever it is, you need to add a new method there – Sean Patrick Floyd Aug 29 '16 at 11:59
  • ok, lets assume I have done that and I have an article url that looks like this site.com/article-title.html not I have repository method that will give me the article via article-title.html, the questions is when someone clicks on that link what will happen then? it will be served by a controller right? where I need to add some sort of RequestMapping and then that method will call the repository method to get the article by url – Toseef Zafar Aug 29 '16 at 12:18
  • I dont think you understood the original question. its the "/article/" bit which is the issue, if you look at my question I want to be able to form url like site.com/article.html in your solution I need you have a fixed bit in the urls i.e. /article/ and secondly If I dont have the {id} in request mapping then is there any other way to resolve this issue? or I have to pick up the article by its title or permalink? – Toseef Zafar Aug 29 '16 at 13:18
  • @Tefa you cen get rid of the /article bit, that doesn't make a difference `@RequestMapping(value="/{permalink}.html")` – Sean Patrick Floyd Aug 29 '16 at 13:56
  • you cant do "/{permalink}.html" I have found a work around you can do this by adding this: /**/{permalink.html – Toseef Zafar Aug 29 '16 at 14:05
  • not sure if you voted down my question @Sean, but if you did then I dont understand why? – Toseef Zafar Sep 03 '16 at 00:06
  • @Tefa what? Why would I do that a week later? – Sean Patrick Floyd Sep 03 '16 at 06:08
  • lol, I dont know why something would do that when they didnt even answer the question. do you think I can do something to flag my question as "legitimate" question and get that -1 reverted back? – Toseef Zafar Sep 04 '16 at 00:10
  • No, downvotes happen, you'll get used to it. No big deal. – Sean Patrick Floyd Sep 04 '16 at 10:03
0

There is no way send hidden id obviously, so it has to be done through a permalink of the article or simply via title, to achieve site.com/title.html you need to get rid of all the fixed bits by adding this request mapping rule:

@RequestMapping(value = "/**/{articleTitle}.html"

but to get the article you can obviously use id as its not there in the URL and have to work with that articleTitle or generate a permalink as suggested by @Sean above.

Toseef Zafar
  • 1,601
  • 4
  • 28
  • 46